Android基本组件之单选按钮和复选框(自用)
来源:互联网 发布:生物医学数据库使用 编辑:程序博客网 时间:2024/05/16 08:52
单选按钮和复选框都是继承普通按钮,因此它们都可以直接使用普通按钮支持的各种属性和方法。
单选按钮由
<RadioButton android:text="显示文本" android:id="@+id/ID号" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked=[true|false]//true为被选中状态> </RadioButton>
定义
RadioButton通常要与RadioGroup一起使用,RadioGroup的常用属性与布局管理器常用属性一样,都需要定义android:orientation
这里摘取《Android从入门到精通》的一个例子
layout里面的代码:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/background"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="性别:" android:height="50px" /> <RadioGroup android:id="@+id/radioGroup1" android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content"> <RadioButton android:layout_height="wrap_content" android:id="@+id/radio0" android:text="男" android:layout_width="wrap_content" android:checked="true"/>//这里不需要</RadioButton> <RadioButton android:layout_height="wrap_content" android:id="@+id/radio1" android:text="女" android:layout_width="wrap_content"/> </RadioGroup> <Button android:text="提交" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button></LinearLayout>
在activity中
final RadioGroup sex = (RadioGroup) findViewById(R.id.radioGroup1); //获取单选按钮组 //为单选按钮组添加事件监听 sex.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { RadioButton r = (RadioButton) findViewById(checkedId); //获取被选择的单选按钮 Log.i("单选按钮", "您的选择是:" + r.getText()); } });
复选框
在默认情况下,复选框显示为一个方块图标,并且在该图标旁边放置一些说明性文字。与单选按钮唯一不同的是,复选框可以进行多选设置,每一个复选框都提供“选中”和“不选中”两种状态。在Android中,可以使用2种方式向屏幕中添加复选框,一种是通过在XML布局文件中使用< CheckBox>标记添加,另一种是在JAVA文件中,通过new关键字创建。跟单选按钮差不多操作
0 0
- Android基本组件之单选按钮和复选框(自用)
- Android组件复习之基本组件02—单选按钮(RadioButton)和复选框(CheckBox)
- Android组件复习之基本组件02—单选按钮(RadioButton)和复选框(CheckBox)
- Android基本组件之按钮(自用)
- android-UI组件实例大全(四)------单选按钮(RadioButton)和复选框(CheckBox)
- Android之复选按钮、单选按钮、开关按钮
- Android 单选按钮和复选框事例
- Android-单选按钮RadioButton和复选框checkbox
- Android中的 单选按钮和复选框
- 菜鸟Android学习之路05——单选按钮和复选框
- 一看就会Android之单选按钮和复选框的事件监听
- 【Android开发】基本组件-单选按钮
- 2复选框和单选按钮
- 单选按钮和复选框整合
- 单选按钮和复选框
- 单选按钮和复选框
- 单选按钮和复选框
- android单选按钮(RadioButton)、复选框(CheckBox)
- 简述jpg、gif、png-8、png-24的区别,分别使用场景
- Oil Deposits UVA - 572(深度优先搜索)
- 初学Java知识点
- UDP服务器和客户端程序实例
- struts2.5通配符映射问题
- Android基本组件之单选按钮和复选框(自用)
- Android contentResolver得到本机联系人数据
- mybatis 批量更新(batchUpdate)
- 【C/C++开发】字符串操作
- 华为机试在线训练-牛客网(16)字符串排序
- PAT:A1001. A+B Format (20/20)
- ConcurrentHashMap用分离锁实现多个线程间的并发写操作
- hdu 4780 Candy Factory(最小费用流)
- Java源码解析(5) —— Class(4)