android RadioButton和CheckBox组件的使用方法
//定义各控件的变量
private TextView who = null;
private TextView how = null;
private RadioGroup who_group = null;
private RadioButton china = null;
private RadioButton america = null;
private RadioButton others = null;
private CheckBox less = null;
private CheckBox thirty = null;
private CheckBox forty = null;
private CheckBox fifty = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获得对应的控件
who = (TextView)findViewById(R.id.who);
how = (TextView)findViewById(R.id.how);
who_group = (RadioGroup)findViewById(R.id.who_group);
china = (RadioButton)findViewById(R.id.china);
america = (RadioButton)findViewById(R.id.america);
others = (RadioButton)findViewById(R.id.others);
less = (CheckBox)findViewById(R.id.less);
thirty = (CheckBox)findViewById(R.id.thirty);
forty = (CheckBox)findViewById(R.id.forty);
fifty = (CheckBox)findViewById(R.id.fifty);
//设置who_group的监听器,其实是一句代码,其参数是一个带有重构函数的对象
who_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if(checkedId == china.getId()){
Toast.makeText(MainActivity.this,"中国", Toast.LENGTH_SHORT).show();
}
else if(checkedId == america.getId()){
Toast.makeText(MainActivity.this, "美国", Toast.LENGTH_SHORT).show();
}
else if(checkedId == others.getId()){
Toast.makeText(MainActivity.this, "其它国家", Toast.LENGTH_SHORT).show();
}
}
});
//下面为4个checkbox多选按钮分别建立监听器
less.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked)
{
Toast.makeText(MainActivity.this, "30个以下", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(MainActivity.this, "不是30个以下", Toast.LENGTH_SHORT).show();
}
}
});
//下面为4个checkbox多选按钮分别建立监听器
thirty.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked)
{
Toast.makeText(MainActivity.this, "30~39", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(MainActivity.this, "不是30~39", Toast.LENGTH_SHORT).show();
}
}
});
//下面为4个checkbox多选按钮分别建立监听器
forty.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked)
{
Toast.makeText(MainActivity.this, "40~49", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(MainActivity.this, "不是40~49", Toast.LENGTH_SHORT).show();
}
}
});
//下面为4个checkbox多选按钮分别建立监听器
fifty.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked)
{
Toast.makeText(MainActivity.this, "50以上", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(MainActivity.this, "不是50以上", Toast.LENGTH_SHORT).show();
}
}
});
- Android组件的使用:RadioButton
- Android MVP模式ListView中嵌入checkBox的使用方法
- 说说Android 两种为自定义组件添加属性的使用方法和区别
- Android组件RadioButton、checkBox、listview、spiner综合实例及Intent传值
- Android高级组件Gallery画廊视图使用方法详解
- [Android]自定义组件示例:使用attrs.xml文件定制RadioButton
- [Android]自定义组件示例:使用attrs.xml文件定制RadioButton
- Android入门(八):使用RadioGroup 和RadioButton组件建立单选清单
- 说说Android 两种为自定义组件添加属性的使用方法和区别
- Android下拉刷新组件Android PullToRefresh使用方法
- Android零基础入门第20节:CheckBox和RadioButton使用大全
- Android零基础入门第20节:CheckBox和RadioButton使用大全
- Android之ExpandableListView(树形组件)的使用方法
- Android SearchView搜索框组件的使用方法
- Android使用原生组件WebView加载网页和数据的方法
- Android开发RadioButton使用方法
- Android重要组件使用方法汇总
- Android RadioButton单选框的使用方法
- TextInput、RadioButton、CheckBox、Combobox组件的使用和实例
- Android 两种为自定义组件添加属性的使用方法和区别