Android组件复习之基本组件02—单选按钮(RadioButton)和复选框(CheckBox)
2016-09-19 15:11
706 查看
单选按钮(RadioButton)和复选框(CheckBox)用法
1、单选按钮(RadioButton)和复选框(CheckBox)是所有用户界面中最普通的UI组件,Android中的RadioButton和CheckBox都继承了Button按钮,因此他们都可以直接使用Button支持的各种属性。2、RadioButton、CheckBox与不同按钮不同的是,他们多了一个可选中的功能,因此RadioButton、CheckBox都可额外指定一个Android:checked属性,该属性用于指定RadioButton、CheckBox初始化是否被选中。
3、RadioButton与CheckBox的不同之外在于,一组RadioButton只能选中其中一个,而复选框可以全选也可以单选。
效果图:
什么都不说了,直接上代码:
布局
<?xml version="1.0" encoding="utf-8"?> <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:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="性别:"/> <!-- 定义一组单选按钮 --> <RadioGroup android:id="@+id/rg" android:layout_height="wrap_content" android:layout_width="wrap_content" android:orientation="vertical" android:layout_gravity="center_horizontal"> <!-- 定义两个单选按钮 --> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/male" android:text="男" android:checked="true"/> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/female" android:text="女"/> </RadioGroup> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="喜欢的颜色:"/> <!-- 定义一个垂直的线性布局 --> <!-- 定义三个复选框 --> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="红色" android:checked="true"/> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="蓝色"/> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="绿色"/> <TextView android:id="@+id/show" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
Activity代码中的引用
import android.app.Activity; import android.os.Bundle; import android.widget.RadioGroup; import android.widget.TextView; public class RadioGroupAndCheckBoxActivity extends Activity { RadioGroup rg; TextView show; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 获取界面上rg、show两个组件 rg = (RadioGroup) findViewById(R.id.rg); show = (TextView) findViewById(R.id.show); // 为RadioGroup组件的OnCheckedChange事件绑定事件监听器 rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // 根据用户勾选的单选按钮来动态改变tip字符串的值 String tip = checkedId == R.id.male ?"您的性别是男人": "您的性别是女人"; // 修改show组件中的文本 show.setText(tip); } }); } }
注意AndroidManifest.xml文件中的配置,启动类改为此类
dome中的位置
dome下载地址:http://download.csdn.net/detail/bobo8945510/9633776
相关文章推荐
- Android组件复习之基本组件02—单选按钮(RadioButton)和复选框(CheckBox)
- android单选按钮(RadioButton)、复选框(CheckBox)
- Android基本组件之单选按钮和复选框(自用)
- Android入门教程二十七之RadioButton(单选按钮)&Checkbox(复选框)
- Android课堂笔记——RadioButton(单选按钮)&Checkbox(复选框)
- Android练习-----单选按钮(RadioButton) && 复选框(CheckBox)
- Android-单选按钮RadioButton和复选框checkbox
- Android基础入门教程——2.3.5.RadioButton(单选按钮)&Checkbox(复选框)
- android-UI组件实例大全(四)------单选按钮(RadioButton)和复选框(CheckBox)
- Android用户界面基础之CheckBox(复选按钮)、RadioButton(单选按钮)学习
- 第7章、单选按钮RadioGroup与复选框CheckBox(从零开始学Android)
- [置顶] Android--(11)--解读单选(RadioButton)和复选(CheckBox)按钮
- android基本控件示例RadioButton单选按钮,一般和RadioGroup一起用(03)
- 单选按钮(RadioButton)与 复选框(CheckBox)的功能与 用法
- 第7章、单选按钮RadioGroup与复选框CheckBox(从零开始学Android)
- 第7章、单选按钮RadioGroup与复选框CheckBox(从零开始学Android)
- RadioButton(单选按钮)+Checkbox(复选框)
- 3.Android之单选按钮RadioGroup和复选框Checkbox学习
- 【Android开发】基本组件-单选按钮
- 单选按钮(RadioButton)与复选框(CheckBox)的功能与用法