第一界面代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listener1 = new OnClickListener() {
public void onClick(View v) {
//开始传值到第二个界面上
Intent intent1 = new Intent(ActivityMain.this, Activity1.class);intent1.putExtra("activityMain", "数据来自activityMain");

//启动新界面并,回调结果
startActivityForResult(intent1, REQUEST_CODE);
}

};

setContentView(R.layout.main);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(listener1);
setTitle("ActivityMain");

}

 

    @Override
//回调函数处理
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE) {
if (resultCode == RESULT_CANCELED)
setTitle("取消");

else if (resultCode == RESULT_OK) {
String temp=null;
Bundle extras = data.getExtras();
       if (extras != null) {
       temp = extras.getString("store");
       }

setTitle(temp);
}
}

第二界面代码:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity1);
        listener1 = new OnClickListener() {
public void onClick(View v) {
                                //向主界面返回数据信息
Bundle bundle = new Bundle();
bundle.putString("store", "数据来自Activity1");
Intent mIntent = new Intent();
mIntent.putExtras(bundle);
setResult(RESULT_OK, mIntent);

finish();

}

};

               //处理从主界面传过来的信息
button1 = (Button) findViewById(R.id.button3);
button1.setOnClickListener(listener1);
String data=null; Bundle extras = getIntent().getExtras();

       if (extras != null) {
            data = extras.getString("activityMain");
       }
setTitle("现在是在Activity1里:"+data);

    }

最后修改:2011 年 07 月 08 日
一分也是爱