关于TextView返回值不显示的问题
当第二个Activity中的button点击后会跳到第一个Activity 但是TextView里面显示的不是设置的返回值,这是什么原因?
以下是FActivity代码
package com.intentdemo; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class FActivity extends Activity { private Button bt1; private Button bt2; private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.factivity); bt1 = (Button) findViewById(R.id.button1); bt2 = (Button) findViewById(R.id.button2); tv = (TextView) findViewById(R.id.textView1); bt1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(FActivity.this, SActivity.class); startActivity(intent); } }); bt2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(FActivity.this, SActivity.class); startActivityForResult(intent, 1); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub // requestCode请求标志resultCode第二个页面返回的标志data第二个页面回传的数据 super.onActivityResult(requestCode, resultCode, data); if (requestCode == 1 && resultCode == 2) { String content = data.getStringExtra("data"); tv.setText(content); } } }
以下是SActivity代码
package com.intentdemo; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class SActivity extends Activity{ private Button bt; private String content="nihao"; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.sactivity); bt = (Button) findViewById(R.id.button11); bt.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent data = new Intent(); data.putExtra("data", content); setResult(2, data); finish(); } }); } }
以下是第一个activity布局代码
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="第一种" /> <Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="第二种" /> <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="第二个页面回传显示"/> </LinearLayout>
以下是第二个activity布局代码
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/button11" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button" /> </LinearLayout>