Android—RadioButton(单选框)和CheckBox(复选框)
来源:互联网 发布:截面数据回归分析实例 编辑:程序博客网 时间:2024/06/07 22:59
一、RadioButton和CheckBox的区别:
1、单个RadioButton在选中后,通过点击无法变为未选中
单个CheckBox在选中后,通过点击可以变为未选中
2、一组RadioButton,只能同时选中一个
一组CheckBox,能同时选中多个
3、RadioButton在大部分UI框架中默认都以圆形表示
CheckBox在大部分UI框架中默认都以矩形表示
二、RadioButton和RadioGroup的关系:
1、RadioButton表示单个圆形单选框,而RadioGroup是可以容纳多个RadioButton的容器
2、每个RadioGroup中的RadioButton同时只能有一个被选中
3、不同的RadioGroup中的RadioButton互不相干,即如果组A中有一个选中了,组B中依然可以有一个被选中
4、大部分场合下,一个RadioGroup中至少有2个RadioButton
5、RadioGroup和CheckBox控件设置监听器都是用的setOnCheckedChangeListener函数,其输入参数是一个函数,且函数内部要实现1个内部类。RadioGroup监听器的输入参数用的是RadioGroup.OnCheckedChangeListener(),而CheckBox监听器的输入参数用的是函数CompoundButton.OnCheckedChangeListener().
代码:
MainActivity:
package com.turo.checkboxtest;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener{ //定义各控件的变量 private TextView who = null; private TextView how = null; private RadioGroup who_group = null; private RadioButton china = null; private RadioButton america = null; private RadioButton others = null; private CheckBox cinema = null; private CheckBox computer = null; private CheckBox DVD = null; private CheckBox phone = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获得对应的控件 who = (TextView)findViewById(R.id.who); how = (TextView)findViewById(R.id.how); who_group = (RadioGroup)findViewById(R.id.who_group); china = (RadioButton)findViewById(R.id.china); america = (RadioButton)findViewById(R.id.america); others = (RadioButton)findViewById(R.id.others); cinema = (CheckBox)findViewById(R.id.cinema_bc); computer = (CheckBox)findViewById(R.id.computer_bc); DVD = (CheckBox)findViewById(R.id.DVD_bc); phone = (CheckBox)findViewById(R.id.phone_bc); cinema.setOnCheckedChangeListener(this); computer.setOnCheckedChangeListener(this); DVD.setOnCheckedChangeListener(this); phone.setOnCheckedChangeListener(this); //设置who_group的监听器,其实是一句代码,其参数是一个带有重构函数的对象 who_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { public void onCheckedChanged(RadioGroup group, int checkedId) { // TODO Auto-generated method stub if(checkedId == china.getId()){ Toast.makeText(MainActivity.this,"中国", Toast.LENGTH_SHORT).show(); } else if(checkedId == america.getId()){ Toast.makeText(MainActivity.this, "美国", Toast.LENGTH_SHORT).show(); } else if(checkedId == others.getId()){ Toast.makeText(MainActivity.this, "其它国家", Toast.LENGTH_SHORT).show(); } } }); } @Override public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) { if(cinema.isChecked()){ Toast.makeText(MainActivity.this,"电影院观看",Toast.LENGTH_SHORT).show(); } if(phone.isChecked()){ Toast.makeText(MainActivity.this,"手机观看",Toast.LENGTH_SHORT).show(); } if(DVD.isChecked()){ Toast.makeText(MainActivity.this,"DVD观看",Toast.LENGTH_SHORT).show(); } if(computer.isChecked()){ Toast.makeText(MainActivity.this,"电脑观看",Toast.LENGTH_SHORT).show(); } }}
activity_main.xml:
<?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:id="@+id/who" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/who" /> <RadioGroup android:id="@+id/who_group" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <RadioButton android:id="@+id/china" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/china" /> <RadioButton android:id="@+id/america" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/america" /> <RadioButton android:id="@+id/others" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/others" /> </RadioGroup> <TextView android:id="@+id/how" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/how" /> <CheckBox android:id="@+id/cinema_bc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/cinema" /> <CheckBox android:id="@+id/computer_bc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/computer" /> <CheckBox android:id="@+id/DVD_bc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/DVD" /> <CheckBox android:id="@+id/phone_bc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/phone" /></LinearLayout>
效果图:
上面3个为一组RadioGroup,每选中其中一个RadioButton,则会有相应的提示。且只能选中其中的一个。
下面的4都为CheckBox,可以选中其中的多个。每个CheckBox被选中都有相应的文字提示小窗口。
- Android—RadioButton(单选框)和CheckBox(复选框)
- 初学单选框RadioButton和复选框CheckBox
- 单选框(RadioButton)和复选框(CheckBox)
- android单选按钮(RadioButton)、复选框(CheckBox)
- Android组件复习之基本组件02—单选按钮(RadioButton)和复选框(CheckBox)
- Android组件复习之基本组件02—单选按钮(RadioButton)和复选框(CheckBox)
- 单选框、复选框RadioButton,checkbox
- ABAP 中checkbox 和 RADIOBUTTON (复选框和单选框)的部分区别
- 单选框(RadioButton)和复选框(CheckBox)的功能与用法
- Android--CheckBox复选框、RadioGroup和RadioButton及其使用
- Android-单选按钮RadioButton和复选框checkbox
- Android UI 详解之单选(RadioButton)和复选(CheckBox)
- 第12章:单选扭(RadioButton)和复选框(CheckBox)的功能和用法
- android基本 UI(三)—单选RadioButton与复选按钮CheckBox
- 【8】CheckBox(复选框)和RadioButton(单选按钮)
- 利用单选钮、复选框获取用户信息(radiobutton checkbox的功能和用法)
- Android--(11)--解读单选(RadioButton)和复选(CheckBox)按钮
- android 复选单选按钮 CheckBox和RadioButton
- .bat批处理学习
- CI连接数据库
- 生活随笔:我在百度阅读挣的20元
- uva 580 Critical Mass(递推)
- 简单button样式的设置(不是直接将背景设置为图片,而是设置背景的颜色)
- Android—RadioButton(单选框)和CheckBox(复选框)
- 我的hibernate学习之旅(2)
- hdu 1253 胜利大逃亡 BFS
- 学习嵌入式的第一天
- 熟练使用java集合001 总览
- try-catch-finally块中的退出语句
- E:in-range伪类选择器与E:out-of-range伪类选择器
- python字典为什么要用clear方法
- 网络库封装