一、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被选中都有相应的文字提示小窗口。