Intent、Bundle——实现Activity之间的通信
http://blog.sina.com.cn/s/blog_62dea93001015847.html
一个应用程序会有多个Activity,但是只有一个Activity作为程序的入口,应用中的其他的Activity只能通过入口Activity来启动,或者由入口Activity启动的Activity启动。(在这里不要晕,可以理解成是一个树结构,入口Activity是根节点)
要用到的两个方法:
- startActivty(Intent intent)
- startActivtyForResult(Intent intent,int requestCode):启动其他Activity,从新启动的Activity中取回结果返回到启动新Activity的Activity。
启动其他Activity需要用到Intent类,在Activity之间传递数据还需要用到Bundle类。下面按Activity的两种启动方法来学习他们的用法。
public class StartActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//获取应用程序中的bn按钮
Button bn = (Button)findViewById(R.id.bn);
//为bn按钮绑定事件监听器
bn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View source)
{
//创建需要启动的Activity对应的Intent
Intent intent = new Intent(StartActivity.this
, SecondActivity.class);
//启动intent对应的Activity
startActivity(intent);
}
});
}
}
===================================================================
public class SecondActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
//获取应用程序中的previous按钮
Button previous = (Button)findViewById(R.id.previous);
//获取应用程序中的close按钮
Button close = (Button)findViewById(R.id.close);
//为previous按钮绑定事件监听器
previous.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View source)
{
//获取启动当前Activity的上一个Intent
Intent intent = new Intent(SecondActivity.this
, StartActivity.class);
//启动intent对应的Activity
startActivity(intent);
}
});
//为close按钮绑定事件监听器
close.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View source)
{
//获取启动当前Activity的上一个Intent
Intent intent = new Intent(SecondActivity.this
, StartActivity.class);
//启动intent对应的Activity
startActivity(intent);
//结束当前Activity
finish();
}
});
}
}
(2)使用Bundle在Activity之间传递数据
public class BundleTest extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button bn = (Button)findViewById(R.id.bn);
bn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
EditText name = (EditText)findViewById(R.id.name);
EditText passwd = (EditText)findViewById(R.id.passwd);
RadioButton male = (RadioButton)findViewById(R.id.male);
String gender = male.isChecked() ? "男 " : "女";
Person p = new Person(name.getText().toString()
, passwd.getText().toString() , gender);
//创建一个Bundle对象
Bundle data = new Bundle();
data.putSerializable("person", p);
//创建一个Intent
Intent intent = new Intent(BundleTest.this
, ResultActivity.class);
intent.putExtras(data);
//启动intent对应的Activity
startActivity(intent);
}
});
}
}
=====================================================================
public class ResultActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
TextView name = (TextView)findViewById(R.id.name);
TextView passwd = (TextView)findViewById(R.id.passwd);
TextView gender = (TextView)findViewById(R.id.gender);
//获取启动该Result的Intent
Intent intent = getIntent();
//获取该intent所携带的数据
Bundle data = intent.getExtras();
//从Bundle数据包中取出数据
Person p = (Person)data.getSerializable("person");
name.setText("您的用户名为:" + p.getName());
passwd.setText("您的密码为:" + p.getPass());
gender.setText("您的性别为:" + p.getGender());
}
}
======================================================================
public class Person implements Serializable
{
private static final long serialVersionUID = 1L;
private Integer id;
private String name;
private String pass;
private String gender;
public Person()
{
}
public Person(String name, String pass, String gender)
{
this.name = name;
this.pass = pass;
this.gender = gender;
}
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getPass()
{
return pass;
}
public void setPass(String pass)
{
this.pass = pass;
}
public String getGender()
{
return gender;
}
public void setGender(String gender)
{
this.gender = gender;
}
}
(3)通过startActivityForResult(Intent intent,int requestCode)启动Activity并返回结果
例子如下:
public class ActivityForResult extends Activity
{
Button bn;
EditText city;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//获取界面上的组件
bn = (Button)findViewById(R.id.bn);
city = (EditText)findViewById(R.id.city);
//为按钮绑定事件监听器
bn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View source)
{
//创建需要对应于目标Activity的Intent
Intent intent = new Intent(ActivityForResult.this
, SelectCityActivity.class);
//启动指定Activity并等待返回的结果,其中0是请求码,用于标识该请求
startActivityForResult(intent , 0);
}
});
}
//重写该方法,该方法以回调的方式来获取指定Activity返回的结果
@Override
public void onActivityResult(int requestCode , int resultCode
, Intent intent)
{
//当requestCode、resultCode同时为0,也就是处理特定的结果
if (requestCode == 0
&& resultCode == 0)
{
//取出Intent里的Extras数据
Bundle data = intent.getExtras();
//取出Bundle中的数据
String resultCity = data.getString("city");
//修改city文本框的内容
city.setText(resultCity);
}
}
}
——————————————————————————————————
public class SelectCityActivity extends ExpandableListActivity
{
//定义省份数组
private String[] provinces = new String[]
{ "广东", "广西", "湖南"};
private String[][] cities = new String[][]
{
{ "广州", "深圳", "珠海", "中山" },
{ "桂林", "柳州", "南宁", "北海" },
{ "长沙", "岳阳" , "衡阳" , "株洲" }
};
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
ExpandableListAdapter adapter = new BaseExpandableListAdapter()
{
//获取指定组位置、指定子列表项处的子列表项数据
@Override
public Object getChild(int groupPosition, int childPosition)
{
return cities[groupPosition][childPosition];
}
@Override
public long getChildId(int groupPosition, int childPosition)
{
return childPosition;
}
@Override
public int getChildrenCount(int groupPosition)
{
return cities[groupPosition].length;
}
private TextView getTextView()
{
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 64);
TextView textView = new TextView(SelectCityActivity.this);
textView.setLayoutParams(lp);
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
textView.setPadding(36, 0, 0, 0);
textView.setTextSize(20);
return textView;
}
//该方法决定每个子选项的外观
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent)
{
TextView textView = getTextView();
textView.setText(getChild(groupPosition, childPosition).toString());
return textView;
}
//获取指定组位置处的组数据
@Override
public Object getGroup(int groupPosition)
{
return provinces[groupPosition];
}
@Override
public int getGroupCount()
{
return provinces.length;
}
@Override
public long getGroupId(int groupPosition)
{
return groupPosition;
}
//该方法决定每个组选项的外观
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent)
{
LinearLayout ll = new LinearLayout(SelectCityActivity.this);
ll.setOrientation(0);
ImageView logo = new ImageView(SelectCityActivity.this);
ll.addView(logo);
TextView textView = getTextView();
textView.setText(getGroup(groupPosition).toString());
ll.addView(textView);
return ll;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition)
{
return true;
}
@Override
public boolean hasStableIds()
{
return true;
}
};
// 设置该窗口显示列表
setListAdapter(adapter);
getExpandableListView().setOnChildClickListener(
new OnChildClickListener()
{
@Override
public boolean onChildClick(ExpandableListView parent, View source,
int groupPosition, int childPosition, long id)
{
//获取启动该Activity之前的Activity对应的Intent
Intent intent = getIntent();
Bundle data = new Bundle();
data.putString("city" ,cities[groupPosition][childPosition]);
intent.putExtras(data);
// 设置该SelectActivity的结果码,并设置结束之后退回的Activity
SelectCityActivity.this.setResult(0 , intent);
//结束SelectCityActivity。
SelectCityActivity.this.finish();
return false;
}
});
}
}