CheckBox 多选按钮的使用方法
来源:互联网 发布:js形参实参如何理解 编辑:程序博客网 时间:2024/05/17 04:06
创建布局文件*声明一组多选按钮,设置各个按钮id .*
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" android:orientation="vertical" >***创建一组多选按钮,设置各个按钮id ,统一由allcheck(全选按钮控制)*** <CheckBox android:id="@+id/eat" android:text="吃饭" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginBottom="5dp" /> <CheckBox android:id="@+id/sleep" android:text="睡觉" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="0dp" android:layout_marginBottom="5dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" /> <CheckBox android:id="@+id/dota" android:text="打dota" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="5dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" />**重点内容** <CheckBox android:id="@+id/allChenked" android:text="全选/全不选" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" /></LinearLayout>
MainActivity 中使用findViewById 获取对象,再为CheckBox 绑定相应的监听器(OnCheckedListener 以及OnCheckedChangeListener(在按钮选中状态发生改变时被调用))
public class MainActivity extends ActionBarActivity { private CheckBox eatBox; private CheckBox sleepBox; private CheckBox dotaBox; private CheckBox allCheckedBox; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);**//此处通过findViewById 找到相应的CheckBox**------------------------------------ eatBox=(CheckBox)findViewById(R.id.eat); sleepBox=(CheckBox)findViewById(R.id.sleep); dotaBox=(CheckBox)findViewById(R.id.dota); allCheckedBox=(CheckBox)findViewById(R.id.allChenked);//生成监听器对象,给AllCheckBoxListener 实现监听器接口 AllCheckBoxListener listener = new AllCheckBoxListener(); allCheckedBox.setOnCheckedChangeListener(listener);//定义AllCheckBoxListener 类,用来实现监听器接口。 class AllCheckBoxListener implements CompoundButton.OnCheckedChangeListener{// CompoundButton.OnCheckedChangeListener 用来监听CheckBox 对象的状态变化 @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) {//该方法内实现当allCheckedBox 状态变化时,eatBox,sleepBox,dataBox 三个CheckBox状态随之变化 eatBox.setChecked(b); sleepBox.setChecked(b); dotaBox.setChecked(b);//或者更改为复杂的代码方式// if (allCheckedBox=isChecked()){// eatbox.setChecked(ture);// sleepbox.setChecked(ture);// dotabox.setChecked(ture);// }else{// eatbox.setChecked(false);// sleepbox.setChecked(false);// dotabox.setChecked(false);// }// } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); }}
0 0
- CheckBox 多选按钮的使用方法
- RadioGroup、RadioButton(单选按钮)、CheckBox(多选按钮)和Toast的使用方法
- Checkbox多选按钮的使用
- CheckBox多选控件的使用方法
- 多选按钮CheckBox实例
- Android CheckBox 多选按钮
- struts中获取多选按钮html:checkbox的值
- 从零开始学android<CheckBox多选按钮的使用.八.>
- 多选按钮CheckBox及全选的实现
- 单选按钮radioButton 多选按钮checkBox Toast 的使用
- 12.3多选按钮(CheckBox)
- CheckBox多选按钮实现CompoundButton.OnCheckedChangeListener
- android多选按钮(CheckBox)
- QT 的单选按钮使用方法
- checkbox全选按钮的控制
- Checkbox与RadioGroup的使用方法
- Android程序:checkbox的使用方法
- Android之多选按钮CheckBox
- 基于Theano的深度学习(Deep Learning)框架Keras学习随笔-11-数据集
- 使用Spring的@Scheduled实现定时任务
- Android应用开发---面试题
- UI技巧 批量修改控件属性
- 2.9.1 初始化进程0
- CheckBox 多选按钮的使用方法
- Ruby学习-Ruby类的使用
- uft的技巧
- 从上往下打印二叉树
- 构建基于Spark的推荐引擎(python实现)
- Apache JMeter中正则表达式提取器的使用方法
- 提高单片机定时器精度的算法
- 原型模式之深浅复制
- vbs学到的几个常用函数