【按住你的心】——Android开发CheckBoxRadioButton控件的简单使用
Android的控件很多,我们从最常用的一些控件学起,今天我们学习CheckBox和RadioButton。
首先,我们要在main.xml文件中添加控件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><RadioGroupandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="vertical" ><RadioButtonandroid:id="@+id/female"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/female" /><RadioButtonandroid:id="@+id/male"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/male" /></RadioGroup><CheckBoxandroid:id="@+id/checkbox"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/car" /></LinearLayout>
这里的RadioButton是在RadioGroup下的两个单选项,我们知道在应用中,单选按钮通常都是只能选一个,所以你可以设置一个RadioGroup中有几个单选按钮,况且在运行的时候在该组下你只能选择一项。
对应的string.xml文件:
<?xml version="1.0" encoding="utf-8"?> <resources><string name="hello">Hello World, CheckBox_RadioButtonActivity!</string><string name="app_name">CheckBox_RadioButton</string><string name="female">女</string><string name="male">男</string><string name="car">汽车</string> </resources>
剩下我们所要做的事情就是在Activity文件中为单选和多选按钮添加监听了,源码如下:
public class CheckBox_RadioButtonActivity extends Activity implementsOnCheckedChangeListener {private RadioButton female;private RadioButton male;private CheckBox cb;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);// 获取CheckBox对象cb = (CheckBox) findViewById(R.id.checkbox);// 获取两个RadioButton对象female = (RadioButton) findViewById(R.id.female);male = (RadioButton) findViewById(R.id.male);// 为CheckBox添加监听cb.setOnCheckedChangeListener(this);// 为RadioButton添加监听 female.setOnClickListener(radio_listener);male.setOnClickListener(radio_listener);}@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {// 对CheckBox的选中状态进行判断并且作出相应的操作if (isChecked == true) {Toast.makeText(getApplicationContext(), cb.getText(),Toast.LENGTH_SHORT).show();} else {Toast.makeText(getApplicationContext(), "you choose cancel",Toast.LENGTH_LONG).show();}}private OnClickListener radio_listener = new OnClickListener() {@Overridepublic void onClick(View v) {// 在RadionButton选中时所要做的操作RadioButton rb = (RadioButton) v;Toast.makeText(getApplicationContext(), rb.getText(),Toast.LENGTH_SHORT).show();}}; }
这里要实现OnCheckedChangeListener这个接口,因为CheckBox的监听器是从CompoundButton继承下来的,所以这里不同于其它控件可以使用回调函数为自身添加监听。
PS:有的时候我们用Eclipse的Alt+?提示功能出不来OnCheckedChangeListener的提示,大家可以在文件头部手动导入下面两个包:
import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener;
之后就会有相应的提示了。
下面我们看运行结果:
1.初始化效果:
2.点击RadionButton男或者女时:
3.选中CheckBox时:
4.取消选中时:
关于点击后Toast的提示我们以后会进行深入的学习。
转载于:https://www.cnblogs.com/H_Razor/archive/2012/05/25/Android05_Vzr_Lou.html
【按住你的心】——Android开发CheckBoxRadioButton控件的简单使用相关推荐
- android开发重要控件,Android界面编程——Android基本控件
Android界面编程 Android应用开发的一项重要内容就是界面开发.对于用户来说,不管APP包含的逻辑多么复杂,功能多么强大,如果没有提供友好的图形交互界面,将很难吸引最终用户. 作为一个程序员 ...
- Android开发--Spinner控件的使用
我们经常会在Windows开发的过程中看到一个下拉菜单控件,在Android中也有相应的控件,它的名字叫Spinner,本文介绍Spinner的用法. 首先,下面的这张截图是实现的实例: 在第一行,你 ...
- Android开发CheckBox控件,全选,反选,取消全选
在Android开发中我们经常会使用CheckBox控件,那么怎么实现CheckBox控件的全选,反选呢 首先布局我们的界面: <?xml version="1.0" enc ...
- Android开发-列表控件
列表控件是Android中最常见的控件之一 由于手机屏幕空间都比较有限,能够一次性在屏幕上显示的内容并不多,当我们的程序中有大量的数据需要展示的时候,就可以借助各种列表控件来实现. <ListV ...
- android 自定义listview控件,一个简单又完整的自定义ListView
ListView 一.简单列表 1.在activity_main中添加控件ListView xmlns:tools="http://schemas.android.com/tools&quo ...
- listview控件Android,Android中ListView控件的简单使用
文章引自郭霖<第一行代码> ListView允许用户通过手指上下滑动的方式将屏幕外的数据滚动到屏幕内,同时屏幕上原有的数据则会滚动出屏幕 使用LIstView控件 1 在布局文件中引入Li ...
- Android开发——RadioButton控件
一,简介 RadioButton(单选按钮) 如题单选按钮,就是只能够选中一个,所以我们需要把RadioButton放到RadioGroup按钮组中,从而实现 单选功能!先熟悉下如何使用RadioBu ...
- Android开发-Spinner控件的使用,spinner设置默认选项
1 简介: Spinner相当于下拉列表,每一个下拉项都可以供用户选择.Spinner可以是弹出对话框模式,也可以是当前页面显示. 2 效果图: 左图:android:spinnerMode=&quo ...
- Android游戏开发系统控件-CheckBox
Android游戏开发系统控件-CheckBox 2012/5/11 星期五 CheckBox是Android系统最普通的UI控件,继承了Button按钮 下面通过一个实例来学习 作者:wwj 功能: ...
最新文章
- 在C#中,如何将一种编码的字符串转换成另外一种编码。
- C-二维数组,多维数组
- java中构造方法和方法全面解析
- 浏览器左上角的网站图标
- Unity3D 预设打包的注意事项
- 记录webpack使用问题,使用报错“UnhandledPromiseRejectionWarning,file-loader图片过大,无法加载图片,打包html文件报错TypeError
- Angularjs1.x 中的 constant, value
- 程序员基本功05表达式中的陷阱
- c语言学习-自定义函数并调用求1-100的累计和
- html5 history api_window.history的跳转探索
- 重构java和js版_重构Javascript代码示例(重构前后对比)
- oracle卸载客户端,oracle11g客户端如何完全卸载
- Qt信号和槽机制详解
- iexplore.exe命令行参数解释
- 项目管理经验-豆知识
- 《禅与摩托车维修艺术》读后感
- CentOS7部署网盘网站
- 关于left / right / stereo / mono
- 《算法竞赛进阶指南》tarjan做法 银河
- 淘宝网站简易分析评价
热门文章
- ExtJS4.2学习(八)表格限制输入数据的类型
- wamp如何更改网站根目录DocumentRoot
- 网络安全体系 应用学习手册 下载
- iOS:融云即时通讯快速集成
- 《Adobe Photoshop CS5中文版经典教程(全彩版)》—第2课2.8节使用仿制图章工具修复特定区域...
- 编译安装keepalived-1.2.2.tar.gz报错处理
- OS study plan
- 构建 编译和运行Urho3D工程
- 第四节 莎士比亚模板
- ReactNative项目打包(Android IOS)