程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> menu-【數據在2個activity之間傳遞】使用Intent連接2個activity,但數據無法在之間傳遞

menu-【數據在2個activity之間傳遞】使用Intent連接2個activity,但數據無法在之間傳遞

編輯:編程綜合問答
【數據在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,希望能幫助到你。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved