RadioGroup、RadioButton、CheckBox、Toast用法
2015-07-18 23:10
375 查看
xml布局文件如下:
<RadioGroup
android:id="@+id/sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/number2"
android:orientation="vertical">
<RadioButton
android:id="@+id/female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"/>
<RadioButton
android:id="@+id/male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"/>
</RadioGroup>
<CheckBox
android:id="@+id/swim"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/sex"
android:text="游泳"/>
<CheckBox
android:id="@+id/football"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/swim"
android:text="足球"/>
MainActivity.java的OnCreate方法中相应的代码如下:
genderGroup = (RadioGroup)findViewById(R.id.sex);
femaleButton = (RadioButton)findViewById(R.id.female);
maleButton = (RadioButton)findViewById(R.id.male);
genderGroup.setOnCheckedChangeListener(new GenderGroupListener());
swimBox = (CheckBox)findViewById(R.id.swim);
footBallBox = (CheckBox)findViewById(R.id.football);
swimBox.setOnCheckedChangeListener(new HobbykBoxListener());
footBallBox.setOnCheckedChangeListener(new HobbykBoxListener());
定义genderGroup、CheckBox的监听器,注意二者的监听器的参数不同:
class GenderGroupListener implements OnCheckedChangeListener{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
//group点击的组的对象,checkedId组中的RadioButton对象的ID
if(femaleButton.getId() == checkedId){
Toast.makeText(MainActivity.this, "女", Toast.LENGTH_SHORT).show();
}
else if(maleButton.getId() == checkedId){
Toast.makeText(MainActivity.this, "男", Toast.LENGTH_SHORT).show();
}
}
}
class HobbykBoxListener implements android.widget.CompoundButton.OnCheckedChangeListener{
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
// TODO Auto-generated method stub
//isChecked是否选中,如果选中则传入真,否则传入假
if(isChecked){
Toast.makeText(MainActivity.this, buttonView.getText().toString(), Toast.LENGTH_SHORT).show();
}
}
}
<RadioGroup
android:id="@+id/sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/number2"
android:orientation="vertical">
<RadioButton
android:id="@+id/female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"/>
<RadioButton
android:id="@+id/male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"/>
</RadioGroup>
<CheckBox
android:id="@+id/swim"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/sex"
android:text="游泳"/>
<CheckBox
android:id="@+id/football"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/swim"
android:text="足球"/>
MainActivity.java的OnCreate方法中相应的代码如下:
genderGroup = (RadioGroup)findViewById(R.id.sex);
femaleButton = (RadioButton)findViewById(R.id.female);
maleButton = (RadioButton)findViewById(R.id.male);
genderGroup.setOnCheckedChangeListener(new GenderGroupListener());
swimBox = (CheckBox)findViewById(R.id.swim);
footBallBox = (CheckBox)findViewById(R.id.football);
swimBox.setOnCheckedChangeListener(new HobbykBoxListener());
footBallBox.setOnCheckedChangeListener(new HobbykBoxListener());
定义genderGroup、CheckBox的监听器,注意二者的监听器的参数不同:
class GenderGroupListener implements OnCheckedChangeListener{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
//group点击的组的对象,checkedId组中的RadioButton对象的ID
if(femaleButton.getId() == checkedId){
Toast.makeText(MainActivity.this, "女", Toast.LENGTH_SHORT).show();
}
else if(maleButton.getId() == checkedId){
Toast.makeText(MainActivity.this, "男", Toast.LENGTH_SHORT).show();
}
}
}
class HobbykBoxListener implements android.widget.CompoundButton.OnCheckedChangeListener{
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
// TODO Auto-generated method stub
//isChecked是否选中,如果选中则传入真,否则传入假
if(isChecked){
Toast.makeText(MainActivity.this, buttonView.getText().toString(), Toast.LENGTH_SHORT).show();
}
}
}
相关文章推荐
- [BZOJ1058][ZJOI2007]报表统计
- 双向循环链表
- c++虚函数表解析
- GCD多线程的使用(二)
- 深入分析Cocos2d-x 2.0中的“纹理”
- Markdown 的一个例子
- #24 Swap Nodes in Pairs
- iOS bug处理
- JS Call()与Apply()
- java对字符的编码处理
- SpringMVC入门配置web.config及applicationContent-mvc.xml
- 转:线程池原理及创建(C++实现)
- word中的图片如何导入博客?
- KVM虚拟化平台部署及管理
- LeetCode 231 Power of Two
- Markdown语法简介
- TabHost主界面实现
- UIKit框架结构
- C#获取当前函数名称
- 煎饼侠观后感