问题描述
- 【数据在2个activity之间传递】使用Intent连接2个activity,但数据无法在之间传递
-
//Activity_Main 代码
public class Menuer extends Activity {
protected static final int REQUEST_CODE = 0;TextView tv; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.menu); tv=(TextView) findViewById(R.id.tv1); Button btn1 = (Button) findViewById(R.id.btn1); btn1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent i = new Intent(getBaseContext(),Activity_A.class); startActivityForResult(i,REQUEST_CODE); } }); } protected void onActivityResult(int requestCode,int resultCode, Intent i) { if(requestCode == REQUEST_CODE) { if(resultCode ==RESULT_OK) { Bundle bundle = i.getExtras(); final String name = bundle.getString("name"); final String sex = bundle.getString("sex"); final String age = bundle.getString("age"); tv.setText("Your messages as follow: n" + "name: " + name +"n sex: " + sex + "n age: " + age); } } super.onActivityResult(0, 0, i); } //Activity_A 代码 public class Activity_A extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_a); EditText name_in = (EditText) findViewById(R.id.name_in); EditText sex_in = (EditText) findViewById(R.id.sex_in); EditText age_in = (EditText) findViewById(R.id.age_in); final String name = name_in.getText().toString(); final String sex = sex_in.getText().toString(); final String age = age_in.getText().toString(); Button btn1 = (Button) findViewById(R.id.btn1); btn1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent i = new Intent(); Bundle bundle = new Bundle(); bundle.putString("name",name); bundle.putString("sex", sex); bundle.putString("age", age); i.putExtras(bundle); setResult(RESULT_OK,i); finish(); } }); }
}
解决方案
你的代码在我的浏览器显示有点问题,请参考我的博客:http://blog.csdn.net/afandaafandaafanda/article/details/41594125,希望能帮助到你。
时间: 2024-11-23 03:01:08