Android的UI组件复选框控件CheckBox

“请选择你喜欢的网站?A、ATAAW.COM B、Google.com C、Android.com”,没错,当我们在软件实现以上内容供用户选项,当然要使用的是复选项控件,那么在Android的界面开发中我们使用哪个控件呢?答案就是Android的复选框CheckBox了。那么,具体如何实现呢,ATAAW.COM来稍做介绍。

既然是复选框,当然具备选中跟未选中状态,我们可以根据控件是否被选中来进行相应的操作,通过对复选框加载时间监听器,来对控件状态的改变作出Actions,当然也可以只对控件是否被选中做判断即可,而在其他控件的监听处理中执行其他操作,这个就根据具体的业务需求来选择。

我们先在布局文件中声明创建3个复选框控件,分别称为“WWW”,“ATAAW”,“COM”。
<CheckBox android:text="WWW" android:id="@+id/ataaw1"
android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox>
<CheckBox android:text="ATAAW" android:id="@+id/ataaw2"
android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox>
<CheckBox android:text="COM" android:id="@+id/ataaw3"
android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox>

为以上复选框添加时间监听器,为了方便起见,我们这里为三个复选框添加同一个时间监听器,通过判断其响应的ID确定哪一个复选框被选中。
A、首先定义监听器
OnCheckedChangeListener listener = new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch(buttonView.getId()){
case R.id.ataaw1: //action
break;
case R.id.ataaw2: //action
break;
case R.id.ataaw3: //action
break;
}
}
}
B、指定监听器
CheckBox www = (CheckBox) this.findViewById(R.id.ataaw1);
CheckBox ataaw = (CheckBox) this.findViewById(R.id.ataaw2);
CheckBox com = (CheckBox) this.findViewById(R.id.ataaw3);
www.setOnCheckedChangeListener(listener);
ataaw.setOnCheckedChangeListener(listener);
com.setOnCheckedChangeListener(listener);

以上即是Android开发中复选框的基本使用方法。




凤凰涅槃/浴火重生/马不停蹄/只争朝夕
隐姓埋名/低调华丽/简单生活/完美人生

更多相关文章

  1. 三、安卓UI学习(1)
  2. android用户界面之按钮(Button)教程实例汇
  3. 在Fragment中设置控件点击方法,执行失败。
  4. TabHost与RadioGroup结合完成的菜单【带效果图】5个Activity
  5. Android常用控件
  6. android用户界面-组件Widget-画廊视图Gallery
  7. 总目录
  8. android 单元测试
  9. [android]在上下文菜单的选中事件中获取列表选中的元素

随机推荐

  1. Clion激活码免费提取,更新于2021年3月15日
  2. Linux进程管理工具 Supervisor详解
  3. Python3高级核心技术97讲
  4. K8s-Pod生命周期
  5. Elasticsearch基本CURD操作
  6. Activiti6.0工作流引擎深度解析
  7. 网易 SaaS 产品精益之路 | 从越来越多的
  8. Elasticsearch中的切词怎么实现的?
  9. ZooKeeper原理详解及常用操作
  10. MySQL性能半同步复制VS异步复制