一、Intent的putExtra(Bundle bundle)向Intent对象中放入需要携带的数据
1.创建Activity之间的信使Intent对象
import android.content.Intent;
Intent intent = new Intent(Activity1.this,Activity2.class);
2.创建需要携带的数据(Bundle对象)
import android.os.Bundle;
Bundle bundle = new Bundle();
bundle.putSerializable(string,object);(string-String类型的,object-对象)
3.向Intent对象中放入数据
intent.putExtras(bundle);
二、Activity2获取Activity1传来的数据:
1.获得启动Activity2的Intent对象.在Activity2中调用getIntent()方法,代码如下:
Intent intent = getIntent();
2.获得传过来的数据(Bundle对象)
Bundle bundle = intent.getExtras();
3.将原来的数据还原
Object object = (Object)bundle.getSerializable("string");(string是Activity1中定义的string)与 bundle.putSerializable(string,object);相对应。
android中Activity之间的数据交换常用的类和方法有:
类:
android.content.Intent
android.os.Bundle
方法:
类Bundle中的
public void putSerializable (String key, Serializable value)
public Serializable getSerializable (String key)
类Intent中的
public Bundle getExtras ()
public Intent putExtras (Bundle extras)
三、测试
1.在测试时,如何实现一个提示
可以使用
Toast.makeText(this, "这是一个提示", Toast.LENGTH_SHORT).show(); //从资源文件string.xml 里面取提示信息
Toast.makeText(this, getString(R.string.welcome), Toast.LENGTH_SHORT).show(); //这个提示会几秒钟后消失
2.可以使用AlertDialog.Builder 才产生一个提示框.
- messagebox形式的
new AlertDialog.Builder(this)
.setTitle("Android 提示")
.setMessage("这是一个提示,请确定")
.show(); - 带一个确定的对话框
new AlertDialog.Builder(this)
.setMessage("这是第二个提示")
.setPositiveButton("确定",
new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialoginterface, int i){
//按钮事件
}
})
.show(); - AlertDialog.Builder 复杂的用法:确定/取消对话框
new AlertDialog.Builder(this)
.setTitle("提示")
.setMessage("确定退出?")
.setIcon(R.drawable.quit)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
setResult(RESULT_OK);//确定按钮事件
finish();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//取消按钮事件
}
})
.show();
3.menu 的用法.
public static final int ITEM_1_ID = Menu.FIRST;
public static final int ITEM_2_ID = Menu.FIRST + 1;
public static final int ITEM_3_ID = Menu.FIRST + 2;
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
//不带图标的menu
menu.add(0, ITEM_1_ID, 0, "item-1");
//带图标的menu
menu.add(0, ITEM_2_ID, 1, "item-2").setIcon(R.drawable.editbills2);
menu.add(0, ITEM_3_ID, 2, "item-3").setIcon(R.drawable.billsum1);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()) {
case 1:
Toast.makeText(this, "menu1",Toast.LENGTH_SHORT).show();
return true;
case 2:
return true;
case 3:
return true;
}
return false;
}
4.Activity 的切换
- Activity 的切换,没有数据传递
//从A到B
Intent intent = new Intent();
intent.setClass(A.this, B.class);
startActivity(intent);
- A到B,B得到A中数据
//启动方A:点击btn_sub传递 picHeight.getText()和 picWeight.getText()
private void setListeners(){
btn_sub.setOnClickListener(sendBund);
}
private OnClickListener sendBund = new OnClickListener(){
public void onClick(View v) {
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putString("key_height", picHeight.getText().toString());
bundle.putString("key_weight", picWeight.getText().toString());
intent.setClass(ActivityMain.this,Report.class);
intent.putExtras(bundle);
startActivity(intent);
}
};
//接收方B
Bundle bundle = new Bundle();
bundle = this.getIntent().getExtras();
double height = Double.parseDouble(bundle.getString("key_height"))/100;
double weight = Double.parseDouble(bundle.getString("key_weight"));
- A到B,A从B得到数据
//A到B
static final int RG_REQUEST = 0;
Intent intent = new Intent();
intent.setClass(A.this, B.class);
startActivityForResult(intent,RG_REQUEST);
//在B中处理
Bundle bundle = new Bundle();
bundle.putString("DataKey", edittext.getText().toString());//给bundle 写入数据
Intent mIntent = new Intent();
mIntent.putExtras(bundle);
setResult(RESULT_OK, mIntent);
finish();
//最后在A的回调函数里面接收数据
if (requestCode == RG_REQUEST) {
if (resultCode == RESULT_CANCELED)
setTitle("Canceled...");
else if(resultCode == RESULT_OK) {
setTitle((String)data.getCharSequenceExtra("DataKey"));
}
}