2 sinat 24077755 sinat_24077755 于 2014.12.07 11:24 提问

【数据在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个回答

afandaafandaafanda
afandaafandaafanda   2014.12.07 22:59
已采纳

你的代码在我的浏览器显示有点问题,请参考我的博客:http://blog.csdn.net/afandaafandaafanda/article/details/41594125,希望能帮助到你。

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!