0

I am at the beginning of an app, I'm an absolute amateur and what i want to do is simple. I am trying to pull Extras from an intent on my first activity into a textview on my second activity First page: what is your name? (type a name and push a button) On the next page: okay so your name is ...(the name they entered) I had an answer to this but had a little trouble understanding it, in the code i have, when i click the okay button just the word okay shows up in the textview rather than the edited text here is my code....

main.java

Button ok;
EditText name;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name=(EditText)findViewById(R.id.editText);
ok=(Button)findViewById(R.id.button);

ok.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View arg0) {
        String a=ok.getText().toString();
        Intent intent = new Intent(getApplicationContext(), secondactivity.class);
        intent.putExtra("NAMEDATA",a);
        startActivity(intent);

    }
});


 }

main.xml

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/okay_button"
android:id="@+id/button"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:textStyle="bold|italic"
android:textSize="23sp"/>

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/editText"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignBottom="@+id/button"
android:hint="@string/text"
android:textSize="25sp"
android:textStyle="bold|italic"
android:layout_toLeftOf="@+id/button"/>

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true"
android:src="@drawable/start"
android:contentDescription="@string/startimage"
android:layout_below="@+id/button"
android:paddingBottom="20dp"
android:paddingTop="20dp"/>

</RelativeLayout>

secondactivity.java

public class secondactivity extends Activity {
TextView t;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
t = (TextView)findViewById(R.id.textView3);
String n = this.getIntent().getStringExtra("NAMEDATA");
t.setText(n);


}

activity_second.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".SecondActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/name_sent"
android:textSize="23sp"
android:id="@+id/textView2"
android:textStyle="bold|italic"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/what_to_do"
android:id="@+id/textView"
android:layout_marginTop="13dp"
android:textSize="23sp"
android:layout_below="@+id/textView2"
android:textStyle="bold|italic"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/bath_time"
android:id="@+id/button"
android:layout_marginTop="29dp"
android:layout_below="@+id/textView"
android:layout_alignRight="@+id/textView"
android:layout_alignLeft="@+id/textView"
android:textSize="25sp"
android:textStyle="bold|italic"
android:onClick="viewimage"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/school"
android:id="@+id/button2"
android:layout_below="@+id/button"
android:layout_alignRight="@+id/button"
android:layout_alignLeft="@+id/button"
android:textSize="25sp"
android:textStyle="bold|italic"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/bed"
android:id="@+id/button3"
android:layout_below="@+id/button2"
android:layout_alignLeft="@+id/button2"
android:layout_alignRight="@+id/button2"
android:textSize="25sp"
android:textStyle="bold|italic"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView3"
android:layout_alignTop="@+id/textView2"
android:layout_toRightOf="@+id/textView2"
android:layout_alignBottom="@+id/textView2"
android:paddingStart="4dp"
android:layout_alignParentRight="true"
android:textSize="23dp"
android:textStyle="bold|italic" />

</RelativeLayout>

androidmainfest.xml

<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="18" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
    android:name=".main"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<activity
    android:name=".secondactivity"
    android:label="@string/title_activity_second" >
</activity>
<activity
    android:name=".bathactivity"
    android:label="@string/title_activity_bath"
    android:parentActivityName="com.example.myapplication.SecondActivity" >
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.example.myapplication.SecondActivity" />
</activity>
</application>

can anyone tell me where I've gone wrong? Think its in my onCreate method on either Activity but really stuck, many thanks

4

1 回答 1

1

实际上 - 你没有从小EditText部件传递任何东西,你只是从Button

问题出在这里:

    String a=ok.getText().toString();
    Intent intent = new Intent(getApplicationContext(), secondactivity.class);
    intent.putExtra("NAMEDATA",a);

您可以EditText以相同的方式将 的内容添加到此字符串的末尾。

尝试这样的事情:

String nameStr = name.getText().toString();
String a = ok.getText().toString();

Intent intent = new Intent(getApplicationContext(), secondactivity.class);
intent.putExtra("NAMEDATA",a+nameStr);
于 2013-09-19T10:58:34.743 回答