Activity傳值的幾種方式
阿新 • • 發佈:2018-10-13
獲取值 類名 star out override *** 演示 rac 技術分享
***Activity的傳值
*第一種:Intent傳值
主xml文件中設置按鈕,點擊跳轉: <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/btn_name" android:onClick="send"/> //設置點擊跳轉事件 主Java代碼中傳值: public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } //點擊按鈕傳遞數據到指定activity中 public void send(View view){ Intent intent = new Intent(MainActivity.this, OtherActivity.class); intent.putExtra("name", "張三");//putExtra(String key,value):key表示唯一標示當前值的鍵,value為要傳的數據 intent.putExtra("age", 20); intent.putExtra("sex", ‘男‘); intent.putExtra("bl", true); startActivity(intent); } } 目標活動的xml布局中添加textview來顯示獲取的值,目標活動的java代碼中獲取值: protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_other); tv = (TextView) findViewById(R.id.tv); //1.獲取激活組件的intent對象 Intent intent = getIntent(); //2.根據key獲取傳遞的數據 String name = intent.getStringExtra("name"); int age = intent.getIntExtra("age", 0);//getIntExtra(表示獲取數據的key,如果根據key沒有獲得數據時顯示的默認值) char sex = intent.getCharExtra("sex", ‘ ‘); boolean bl = intent.getBooleanExtra("bl", false); //3.將數據顯示到TextView中 tv.setText("name:"+name+"age:"+age+"sex:"+sex+"bl:"+bl); }
*第二種:Bundle傳值
public void send(View view){ //1.創建Intent意圖對象 Intent intent = new Intent(MainActivity.this, OtherActivity.class); //2.創建bundle對象,用來存儲需要傳遞的數據 Bundle bundle = new Bundle(); //3.將需要傳遞的數據存儲到bundle對象中 bundle.putString("name", "鳳姐"); bundle.putChar("sex", ‘女‘); bundle.putInt("age", 20); //4.將bundle對象存儲到intent對象中 intent.putExtras(bundle); //5.啟動activity startActivity(intent); } protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_other); tv = (TextView) findViewById(R.id.tv); //1.獲取激活組件的intent對象 Intent intent = getIntent(); //2.獲取傳遞的bundle的對象 Bundle bundle = intent.getExtras(); //3.在bundle中根據key獲取傳遞的數據 String name = bundle.getString("name"); int age = bundle.getInt("age"); char sex = bundle.getChar("sex"); //4.將數據顯示到TextView中 tv.setText("name:"+name+"age:"+age+"sex:"+sex); }
*第三種:使用Application全局對象傳值
1.單獨創建一個類繼承Application,將需要存儲的數據定義,並提供set及get方法 2.在發送的activity中存儲數據 3.在接收的activity中獲取數據 4.在清單文件中進行註冊 <application android:name="自定義application的包名.類名"> </application> public class MyApplication extends Application { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } public class MainActivity extends Activity { private MyApplication application; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } //點擊按鈕傳遞數據到目標activity中 public void send(View view){ application = (MyApplication) getApplication(); application.setName("小明"); application.setAge(18); startActivity(new Intent(MainActivity.this, OtherActivity.class)); } } public class OtherActivity extends Activity { private TextView tv; private MyApplication application; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_other); tv = (TextView) findViewById(R.id.tv); application = (MyApplication) getApplication(); String name = application.getName(); int age = application.getAge(); tv.setText("name=" +name +"age="+age); } } <application android:name="com.example.creatactivity.MyApplication"> </application>
*第四種:啟動Activity回傳數據
案例演示:
public class MainActivity extends Activity {
private EditText et_num1,et_num2;
private TextView tv_result;
private static final int REQUEST_CODE=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_num1 = (EditText) findViewById(R.id.editText1);
et_num2 = (EditText) findViewById(R.id.editText2);
tv_result = (TextView) findViewById(R.id.textView3);
}
//點擊按鈕傳遞數據到目標activity中
public void onClick(View view){
Intent intent = new Intent(MainActivity.this, OtherActivity.class);
String num1 = et_num1.getText().toString();
String num2 = et_num2.getText().toString();
intent.putExtra("num1", num1);
intent.putExtra("num2", num2);
startActivityForResult(intent, REQUEST_CODE);//startActivityForResult(請求的intent對象,大於0的整數請求碼)
}
/*
* 該方法用來處理setResult()方法回傳的數據
* int requestCode,請求碼
* int resultCode,結果碼
* Intent data,回傳的intent意圖對象
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_CODE&&resultCode==Activity.RESULT_OK){
String result = data.getStringExtra("info");
tv_result.setText(result);
}
}
}
public class OtherActivity extends Activity {
private TextView tv_num1,tv_num2;
private EditText et_result;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other);
tv_num1 = (TextView) findViewById(R.id.tv1);
tv_num2 = (TextView) findViewById(R.id.tv3);
et_result = (EditText) findViewById(R.id.et1);
Intent intent = getIntent();
tv_num1.setText(intent.getStringExtra("num1"));
tv_num2.setText(intent.getStringExtra("num2"));
}
//點擊按鈕將數據回傳到主activity
public void send(View view){
String result = et_result.getText().toString();//獲取結果數據
Intent intent = new Intent();
intent.putExtra("info", result);
setResult(Activity.RESULT_OK, intent);//setResult(表示請求的結果碼,表示回傳的數據的intent對象)
OtherActivity.this.finish();//關閉當前的activity
}
}
Activity傳值的幾種方式