好记性不如烂笔杆-android学习笔记<三> RadioGroup 和 CheckBox的使用
2012-11-20 16:09
330 查看
8,//RadioGroup 和 CheckBox的使用
xml文件中:
Java文件中
xml文件中:
<RadioGroup android:id="@+id/genderGroup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <RadioButton android:id="@+id/femaleButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/female"/> <RadioButton android:id="@+id/maleButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/male"/> </RadioGroup> <CheckBox android:id="@+id/swim" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/swimBox" /> <CheckBox android:id="@+id/run" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/runBox" /> <CheckBox android:id="@+id/read" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/readBox" />
Java文件中
//为RadioButton添加监听器,一组按钮对应一个监听器 genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { if(famaleButton.getId() == checkedId){ System.out.println("famale"); Toast.makeText(RadioTest.this, "famale", Toast.LENGTH_SHORT).show(); } else if(maleButton.getId() == checkedId){ System.out.println("male"); Toast.makeText(RadioTest.this, "male", Toast.LENGTH_SHORT).show(); } } }); //为多选框添加监听器,每个checkbox对应一个监听器 swimBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked){ System.out.println("swim is checked"); } else { System.out.println("swim is unchecked"); } } }); runBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked){ System.out.println("run is checked"); } else { System.out.println("run is unchecked"); } } }); readBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked){ System.out.println("read is checked"); } else { System.out.println("read is unchecked"); } } }); }
相关文章推荐
- 好记性不如烂笔杆-android学习笔记<十二> Toast的使用
- 好记性不如烂笔杆-android学习笔记<四> 布局用控件简单介绍
- 好记性不如烂笔杆-android学习笔记<十六> switcher和gallery
- 好记性不如烂笔杆-android学习笔记<十四> EditText 画行,解决光标压线问题
- 好记性不如烂笔杆-android学习笔记<五> ListView用法
- 好记性不如烂笔杆-android学习笔记<十五> GridView简单用法
- 好记性不如烂笔杆-android学习笔记<十> Dialog用法
- 好记性不如烂笔杆-android学习笔记<一>
- 好记性不如烂笔杆-android学习笔记<七>Handle用法
- 好记性不如烂笔杆-android学习笔记<十一> Service的应用
- 好记性不如烂笔杆-android学习笔记<八> Broadcase简单用法
- 好记性不如烂笔杆-android学习笔记<九> Content Provider 数据共享机制
- 好记性不如烂笔杆-android学习笔记<二> Acitvity lifecycle 生命周期
- 好记性不如烂笔杆-android学习笔记<十三> Notification
- Spinner、CheckBox、RadioGroup的简单使用
- RadioGroup、RadioButton(单选按钮)、CheckBox(多选按钮)和Toast的使用方法
- Checkbox与RadioGroup的使用方法
- Checkbox与RadioGroup的使用方法
- RadioGroup,RadioButton CheckBox控件的使用
- RadioGroup,Radio单选按钮,CheckBox的使用