RadioButton和CheckBox
来源:互联网 发布:matlab画数据分布图 编辑:程序博客网 时间:2024/05/22 03:08
一:RadioButton
RadioButton为单选按钮,需要和RadioGroup组合使用主要目的:获取用户选择的按钮的值
1.获取RadioGroup控件
radioGroup = (RadioGroup)findViewById(R.id.radiogroup1);2.为radioGroup添加监听事件
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup radioGroup, int i) { //第二个参数i为被选取的RadioButton 资源ID // ID已可以这样获取 int id = radioGroup.getCheckedRadioButtonId(); radioButton = (RadioButton)findViewById(i); tv.setText(radioButton.getText()); } });
1)方法getCheckedRadioButtonId()
获取选中RadioButton的ID2)onCheckedChanged()第二个参数是返回被选取RadioButton的资源ID
二:复选框CheckBox
CheckBox和RadioButton不同(一次只能选取一项),复选框一次可以复选多个目的:要对每一个checkBox做出监听,获取选中和取消的状态
//复选框的资源ID数组 int[] check_id = {R.id.box1, R.id.box2, R.id.box3, R.id.box4}; for(int id:check_id){ CheckBox chk = (CheckBox)findViewById(id); chk.setOnCheckedChangeListener(checklistener);//对每一个checkBox注册监听对象 } } ArrayList<CompoundButton> selected = new ArrayList<>(); //存放被选中的checkbox组件 private CompoundButton.OnCheckedChangeListener checklistener = new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { //参数compoundButton 为被选中的checkbox //参数b 是isChecked是否被选中 //tvbox.setText("你选中了"+compoundButton.getText().toString());输出单个选中项 String msg = ""; if(b){ selected.add(compoundButton); //若选中则添加到集合 for(CompoundButton chk:selected) msg += "\n"+chk.getText().toString(); tvbox.setText("你选中了"+msg); }else{ selected.remove(compoundButton);//若取消则移除集合 tvbox.setText("你取消了"+compoundButton.getText().toString()); } } };onCheckedChanged()方法第一个参数是被触发的checkBox,第二个是触发的状态
阅读全文
0 0
- RadioButton和CheckBox
- CheckBox和RadioButton应用
- RadioButton和CheckBox浅析
- CheckBox和RadioButton
- RadioButton和CheckBox
- RadioButton、CheckBox和ImageView
- Android CheckBox和RadioButton
- Checkbox 和RadioButton
- RadioButton和CheckBox
- RadioButton和CheckBox
- RadioButton和CheckBox
- RadioButton 和 CheckBox 浅析
- RadioButton和CheckBox
- RadioButton和CheckBox
- RadioButton和CheckBox的区别,RadioButton&RadioGroup
- RadioButton 和 CheckBox 和 Toast
- RadioButton和CheckBox的区别
- RadioButton和CheckBox自定义样式
- RecyclerView 的 FastScroller 绘制的源码分析
- angular4表单—模板式表单
- Win10 安装Ant运行build.xml遇到的问题以及解决
- 【Scikit-Learn 中文文档】特征选择
- Storyboard References
- RadioButton和CheckBox
- 如何优雅的打破NSTimer与控制器的循环引用
- 受限玻尔兹曼机
- oracle wm_concat函数的使用
- Qt5实现百度离线地图APIV2_0用QWebChannel实现Qt与js交互
- Metasploit阅读笔记1
- 7.1
- 博客第一次
- Handler或Runnable作为非静态内部类,引发的内存泄露问题