OnCheckedChangeListener(处理用户在选中单选按钮的选项时触发)
RadioGroup rg = ( RadioGroup) findById(new OnCheckedChangeListener(){
Public void OnCheckedChanged (RadioGroup group, int checked){
……
}
});
OnItemSelectedListenerImpl(下拉列表监听器)
1.获得一个下拉列表控件
Spinner spin=(Spinner)findById(R. id. xml文件中Spinner的id);
2.定义一个下拉列表内容适配器
ArrayAdapter<CharSequence> adapter= ArrayAdapter.createFromResource ();
// Adapter . setDropDownViewResource ();//设置列表风格
3.将适配器对象传入spin
spin.setAdapter(adapter);
String text=下拉列表对象名.getSelectedItem().toString();
级联下拉列表的实现
1 public void onCreate(Bundle savedInstanceState) {
2 super.onCreate(savedInstanceState);
3 setContentView(R.layout.main);
4
5 // 定义适配器,读取xml文件中省份的内容
6 ArrayAdapter<CharSequence> adapter =ArrayAdapter.createFromResource(this, R.array.pro,android.R.layout.simple_spinner_item);
7 //设置列表显示风格
8 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
9 //第一个下拉框
10 final Spinner spin = (Spinner)super.findViewById(R.id.Spinner01);
11 spin.setAdapter(adapter); //将省份的适配器对象传入
12 spin.setPrompt("选择省份");
13
14 // 定义适配器,读取xml文件中湖南省城市的内容
15 final ArrayAdapter<CharSequence> c1 = ArrayAdapter.createFromResource(
16 this, R.array.HuNancity, android.R.layout.simple_spinner_item);
17 // 定义适配器,读取xml文件中广东省城市的内容
18 final ArrayAdapter<CharSequence> c2 = ArrayAdapter.createFromResource(
19 this, R.array.guangdongcity,
20 android.R.layout.simple_spinner_item);
21
22 spin.setOnItemSelectedListener(new OnItemSelectedListener() {
23 public void onItemSelected(AdapterView<?> arg0, View view,int position, long id) {
24 final Spinner sp1 = (Spinner)findViewById(R.id.Spinner02);
25 // 读取【省份名称】的下拉框选中内容
26 final String itemPro =spin.getSelectedItem().toString();
27 if (itemPro.equals("湖南省")) {
28 sp1.setAdapter(c1);
29 } else if (itemPro.equals("广东省")) {
30 sp1.setAdapter(c2);
31 }
32 }
33
34 public void onNothingSelected(AdapterView<?> arg0) {}
35 });
36 }