打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Android的UI组件复选框控件CheckBox | 第三极 | 移动开发者

Android的UI组件复选框控件CheckBox

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

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

我们先在布局文件中声明创建3个复选框控件,分别称为“WWW”,“ATAAW”,“COM”。

1            2            3            4            5            6            
<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、首先定义监听器

1            2            3            4            5            6            7            8            9            10            11            12            13            
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、指定监听器

1            2            3            4            5            6            
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开发中复选框的基本使用方法。

原文作者:diordna

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
一网打尽Android UI 控件教程
Android应用程序----UI界面控件(2)CheckBox,RadioButton,Spinner
Android酷炫实用的开源框架(UI框架)
android各种组件的监听器
Android学习笔记16:Button控件图文混排效果的实现
Android Button控件
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服