2010年5月4日 星期二

兩不同Activity間的資料傳遞

老師,同學大家好:
今天來貼一篇關於兩不同Activity間的資料傳遞,

Scenario :
第一個Activity詢問您所就讀的學校,按下確定的Button後會將值傳到第二個Activity,
在第二個Activity按下回上一頁的Button後會將值傳回到第一個Activity。



資料傳遞是使用Bundle方式來進行,因為有回傳資料的部分所以使用onActivityResult區呼叫第二個Activity,這個動作會使第一個Activity生命週期停在onStop等待接收資料而不是onDestroy掉。


首先建立一個名稱為HelloActivity專案,內容如下
HelloActivity.java程式碼如下:
---------------------------------------程式開始------------------------------
package idv.android.HelloActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;

public class HelloActivity extends Activity {
private RadioButton radiobutton1;
private RadioButton radiobutton2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
String school="";
radiobutton1 = (RadioButton) findViewById(R.id.school_1);
radiobutton2 = (RadioButton) findViewById(R.id.school_2);
if(radiobutton1.isChecked()){
school="FCU";
}else{
school="NKUT";
}
/*new一個Intent物件,並指定class*/
Intent intent = new Intent();
intent.setClass(HelloActivity.this,HelloActivity_1.class);
/*將Bundle物件assign給Intent*/
Bundle bundle = new Bundle();
bundle.putString("school",school);
/*將Bundle物件assign給Intent*/
intent.putExtras(bundle);
/*呼叫Activity Activity_1*/
startActivityForResult(intent,0);
}
});
}
protected void onActivityResult(int requestCode, int resultCode,Intent data){
switch (resultCode){
case RESULT_OK:
Bundle bunde = data.getExtras();
String school = bunde.getString("school");
Toast.makeText(this, "接收值:"+school, Toast.LENGTH_LONG).show();
if(school.equals("FCU")){
radiobutton1.setChecked(true);
}else{
radiobutton2.setChecked(true);
}
break;
default:
break;
}
}
}
---------------------------------------程式結束------------------------------

HelloActivity_1.java程式碼如下:
---------------------------------------程式開始------------------------------
package idv.android.HelloActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class HelloActivity_1 extends Activity {
Bundle bunde;
Intent intent;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_1);
/* 取得Intent中的Bundle物件 */
intent=this.getIntent();
bunde = intent.getExtras();
/* 取得Bundle物件中的資料 */
String school = bunde.getString("school");
Toast.makeText(this, "接收值:"+school, Toast.LENGTH_LONG).show();
String schoolText="";
if(school.equals("FCU")){
schoolText="逢甲";
}else{
schoolText="南開";
}
TextView tv1=(TextView) findViewById(R.id.text1);
tv1.setText("您就讀的學校是"+schoolText);
Button b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
/* 回傳result回上一個activity */
HelloActivity_1.this.setResult(RESULT_OK, intent);
/* 關閉activity */
HelloActivity_1.this.finish();
}
});
}
}
---------------------------------------程式結束------------------------------
程式下載:

沒有留言:

張貼留言