sinat_24077755
10086转008
2014-12-07 03:24
采纳率: 66.7%
浏览 1.7k
已采纳

【数据在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();

        }

    });
}

}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

相关推荐