雾山的Anrdoid学习笔记---CheckBox,RadioGroup&RadioButton .
来源:互联网 发布:mysql 双主 英文描述 编辑:程序博客网 时间:2024/06/05 09:06
CheckBox是Android提供的多选按钮控件,它只有true和false两种状态。在xml中默认为false(未选中),也可以这样设置
- android:checked="true"
android:checked="true"
这样的话,就默认选中了。
RadioButton为单选按钮。一个RadioGroup可以包含多个RadioButton。
RadioButton和RadioGroup的关系:
1、RadioButton表示单个圆形单选框,而RadioGroup是可以容纳多个RadioButton的容器
2、每个RadioGroup中的RadioButton同时只能有一个被选中
3、不同的RadioGroup中的RadioButton互不相干,即如果组A中有一个选中了,组B中依然可以有一个被选中
4、大部分场合下,一个RadioGroup中至少有2个RadioButton
5、大部分场合下,一个RadioGroup中的RadioButton默认会有一个被选中,并建议您将它放在RadioGroup中的起始位置
RadioButton和CheckBox的区别:
1、单个RadioButton在选中后,通过点击无法变为未选中
单个CheckBox在选中后,通过点击可以变为未选中
2、一组RadioButton,只能同时选中一个
一组CheckBox,能同时选中多个
3、RadioButton在大部分UI框架中默认都以圆形表示
CheckBox在大部分UI框架中默认都以矩形表示
下面看代码:
开发步骤:
1,新建一个Android应用程序
2,在布局文件中创建两个TextView控件标签,一个RadioGroup控件和四个CheckBox控件,并为其设置属性和值
3,在Activity中,声明所有控件变量并根据id获得控件对象 ,设置RadioButton和CheckBox的监听器,
4,当选中all按钮的时候,要求所有的多选按钮都被选中
- package com.tangbc.choosedemo;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.Menu;
- import android.widget.CheckBox;
- import android.widget.CompoundButton;
- import android.widget.CompoundButton.OnCheckedChangeListener;
- import android.widget.RadioButton;
- import android.widget.RadioGroup;
- import android.widget.Toast;
- public class MainActivity extends Activity {
- private RadioGroup radioGroup;
- private RadioButton boyButton;
- private RadioButton girlButton;
- private CheckBox wowCB;
- private CheckBox lolCB;
- private CheckBox dotaCB;
- private CheckBox allCB;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- //为所有控件绑定id
- radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
- boyButton = (RadioButton) findViewById(R.id.boyButton);
- girlButton = (RadioButton) findViewById(R.id.girlButton);
- wowCB = (CheckBox) findViewById(R.id.wowCheckBox);
- lolCB = (CheckBox) findViewById(R.id.lolCheckBox);
- dotaCB = (CheckBox) findViewById(R.id.dotaCheckBox);
- allCB = (CheckBox) findViewById(R.id.allCheckBox);
- //绑定监听器
- radioGroup.setOnCheckedChangeListener(new RadioGroupListener());
- wowCB.setOnCheckedChangeListener(new CheckBoxListener());
- lolCB.setOnCheckedChangeListener(new CheckBoxListener());
- dotaCB.setOnCheckedChangeListener(new CheckBoxListener());
- allCB.setOnCheckedChangeListener(new CheckBoxListener());
- }
- // 导入android.widget.CompoundButton.OnCheckedChangeListener
- class CheckBoxListener implements OnCheckedChangeListener {
- @Override
- public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
- if (buttonView.getId() == wowCB.getId()) {
- if (isChecked == true) {
- //获取点击按钮的text文本
- String text = buttonView.getText().toString();
- System.out.println(text );
- } else {
- System.out.println("cancelWOW");
- }
- } else if (buttonView.getId() == lolCB.getId()) {
- if (isChecked == true) {
- String text = buttonView.getText().toString();
- System.out.println(text );
- } else {
- System.out.println("cancelLOL");
- }
- } else if (buttonView.getId() == dotaCB.getId()) {
- if (isChecked == true) {
- String text = buttonView.getText().toString();
- System.out.println(text );
- } else {
- System.out.println("cancelDota");
- }
- } else if (buttonView.getId() == allCB.getId()) {
- if (isChecked == true) { //all按钮为选中状态后,其他的按钮都被选中
- System.out.println("all");
- wowCB.setChecked(true);
- lolCB.setChecked(true);
- dotaCB.setChecked(true);
- } else {
- String text = buttonView.getText().toString();
- System.out.println(text );
- }
- } else {
- System.out.println("default");
- }
- }
- }
- // 导入android.widget.RadioGroup.OnCheckedChangeListener
- class RadioGroupListener implements
- android.widget.RadioGroup.OnCheckedChangeListener {
- @Override
- public void onCheckedChanged(RadioGroup group, int checkedId) {
- if (checkedId == boyButton.getId()) {
- System.out.println("boy");
- Toast.makeText(MainActivity.this, "你选的是boy", Toast.LENGTH_SHORT).show();
- } else if (checkedId == girlButton.getId()) {
- System.out.println("girl");
- Toast.makeText(MainActivity.this, "你选的是girl", Toast.LENGTH_SHORT).show();
- }
- }
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- }
package com.tangbc.choosedemo;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.CompoundButton.OnCheckedChangeListener;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.Toast;public class MainActivity extends Activity {private RadioGroup radioGroup;private RadioButton boyButton;private RadioButton girlButton;private CheckBox wowCB;private CheckBox lolCB;private CheckBox dotaCB;private CheckBox allCB;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main); //为所有控件绑定idradioGroup = (RadioGroup) findViewById(R.id.radioGroup);boyButton = (RadioButton) findViewById(R.id.boyButton);girlButton = (RadioButton) findViewById(R.id.girlButton);wowCB = (CheckBox) findViewById(R.id.wowCheckBox);lolCB = (CheckBox) findViewById(R.id.lolCheckBox);dotaCB = (CheckBox) findViewById(R.id.dotaCheckBox);allCB = (CheckBox) findViewById(R.id.allCheckBox); //绑定监听器radioGroup.setOnCheckedChangeListener(new RadioGroupListener());wowCB.setOnCheckedChangeListener(new CheckBoxListener());lolCB.setOnCheckedChangeListener(new CheckBoxListener());dotaCB.setOnCheckedChangeListener(new CheckBoxListener());allCB.setOnCheckedChangeListener(new CheckBoxListener());}// 导入android.widget.CompoundButton.OnCheckedChangeListenerclass CheckBoxListener implements OnCheckedChangeListener {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {if (buttonView.getId() == wowCB.getId()) {if (isChecked == true) {//获取点击按钮的text文本String text = buttonView.getText().toString();System.out.println(text );} else {System.out.println("cancelWOW");}} else if (buttonView.getId() == lolCB.getId()) {if (isChecked == true) {String text = buttonView.getText().toString();System.out.println(text );} else {System.out.println("cancelLOL");}} else if (buttonView.getId() == dotaCB.getId()) {if (isChecked == true) {String text = buttonView.getText().toString();System.out.println(text );} else {System.out.println("cancelDota");}} else if (buttonView.getId() == allCB.getId()) {if (isChecked == true) { //all按钮为选中状态后,其他的按钮都被选中System.out.println("all");wowCB.setChecked(true);lolCB.setChecked(true);dotaCB.setChecked(true);} else {String text = buttonView.getText().toString();System.out.println(text );}} else {System.out.println("default");}}}// 导入android.widget.RadioGroup.OnCheckedChangeListenerclass RadioGroupListener implementsandroid.widget.RadioGroup.OnCheckedChangeListener {@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {if (checkedId == boyButton.getId()) {System.out.println("boy");Toast.makeText(MainActivity.this, "你选的是boy", Toast.LENGTH_SHORT).show();} else if (checkedId == girlButton.getId()) {System.out.println("girl");Toast.makeText(MainActivity.this, "你选的是girl", Toast.LENGTH_SHORT).show();}}}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}
XML文件
- <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context=".MainActivity" >
- <TextView
- android:id="@+id/sexTextView"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="你是男生还是女生" />
- <RadioGroup
- android:id="@+id/radioGroup"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_below="@id/sexTextView" >
- <RadioButton
- android:id="@+id/boyButton"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="boy" />
- <RadioButton
- android:id="@+id/girlButton"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="girl" />
- </RadioGroup>
- <TextView
- android:id="@+id/gameTextView"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_below="@id/radioGroup"
- android:text="你喜欢玩的游戏" />
- <CheckBox
- android:id="@+id/wowCheckBox"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_below="@id/gameTextView"
- android:checked="false"
- android:text="WOW" />
- <CheckBox
- android:id="@+id/lolCheckBox"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_below="@id/wowCheckBox"
- android:checked="false"
- android:text="LOL" />
- <CheckBox
- android:id="@+id/dotaCheckBox"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_below="@id/lolCheckBox"
- android:text="Dota" />
- <CheckBox
- android:id="@+id/allCheckBox"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_below="@id/dotaCheckBox"
- android:text="all" />
- </RelativeLayout>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:id="@+id/sexTextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="你是男生还是女生" /> <RadioGroup android:id="@+id/radioGroup" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/sexTextView" > <RadioButton android:id="@+id/boyButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="boy" /> <RadioButton android:id="@+id/girlButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="girl" /> </RadioGroup> <TextView android:id="@+id/gameTextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/radioGroup" android:text="你喜欢玩的游戏" /> <CheckBox android:id="@+id/wowCheckBox" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/gameTextView" android:checked="false" android:text="WOW" /> <CheckBox android:id="@+id/lolCheckBox" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/wowCheckBox" android:checked="false" android:text="LOL" /> <CheckBox android:id="@+id/dotaCheckBox" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/lolCheckBox" android:text="Dota" /> <CheckBox android:id="@+id/allCheckBox" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/dotaCheckBox" android:text="all" /></RelativeLayout>
点我下载源码
- 雾山的Anrdoid学习笔记---CheckBox,RadioGroup&RadioButton
- 雾山的Anrdoid学习笔记---CheckBox,RadioGroup&RadioButton .
- 雾山的Robotium学习笔记---CheckBox,RadioGroup&RadioButton的测试方法及结果判定
- 雾山的Robotium学习笔记---CheckBox,RadioGroup&RadioButton的测试方法及结果判定 .
- 【Android学习笔记】RadioGroup,RadioButton的使用&CheckBox的使用
- RadioButton和CheckBox的区别,RadioButton&RadioGroup
- 控件RadioGroup、RadioButton和CheckBox的使用
- RadioGroup,RadioButton CheckBox控件的使用
- RadioGroup RadioButton ,checkBox ,menu
- RadioButton RadioGroup CheckBox
- RadioGroup的RadioButton简单用法——学习笔记
- 【幻化万千戏红尘】qianfeng-Android-Day03-RadioButton及RadioGroup的用法、CheckBox、ProgressBar基础学习:
- Android学习笔记之RadioButton(RadioGroup)
- RadioGroup、RadioButton以及Spinner学习笔记
- Android学习笔记之RadioButton(RadioGroup)
- CheckBox和RadioButton以及RadioGroup
- android:RadioGroup RadioButton CheckBox备忘
- Android控件RadioButton&RadioGroup&CheckBox
- Atheros AR9285坑爹网卡只有54M/65M,开启150M速率的方法
- 静态库链接学习
- CSS模块化编码让开发事半功倍
- 20140704-C#object/List<object>与json、xml之间的相互转换
- 字符串时间转化成time_t格式
- 雾山的Anrdoid学习笔记---CheckBox,RadioGroup&RadioButton .
- [Linux筆記] 利用 mount 指令解決 Read-only file system 問題
- 线程之间利用信号量协调对共享资源访问的c++代码实现
- container_of分析
- OTN交换& P-OTN有效降低100G 网络成本 (二)
- c#XML操作
- ZOJ3329-One Person Game(概率DP求数学期望)
- java.util.concurrent介绍
- 我准备在CSDN上写点什么了