Android开发控件之CheckBox、RadioGroup、RadioButton

[b]CheckBox、RadioGroup、RadioButton控件是在Android开发当中做选择的时候用到的控件,下面将逐一介绍这三个控件的用法[/b]
[b]一、复选框CheckBox控件[/b]
1.两种状态
选中状态和未选中状态
2.属性
android:id=”@+id/checkbox” id属性
android:layout_width=”wrap_content” 宽度属性
android:layout_height=”wrapcontent” 高度属性
android:checked=”false” 选中状态为true,未选中为false,默认false
android:text=”男”复选框显示的文本内容
3.使用样例
a.布局文件activity_main.xml代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/lanqiu" />
</LinearLayout>

b.MainActivity java代码
public class MainActivity extends Activity {
private CheckBox checkBox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化CheckBox
checkBox=(CheckBox) findViewById(R.id.checkBox1);
//通过设置CheckBox的监听事件来判断CheckBox是不是被选中了
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// 通过onCheckedChanged来监听当前的checkbox是否被选中
if(isChecked){
//获得checkbox的文本内容
String text=checkBox.getText().toString();
Log.i("tag",text);
}
}
});
}
}

c.方法总结
setOnCheckedChangeListener CheckBox的监听事件的方法,本例中用的是前面介绍的匿名内部类去实现的,当然也可以用其它两种方法去实现。
String text=checkBox.getText().toString();获取CheckBox中的文本内容。
d.运行结果
将程序不知道虚拟机上后,点击CheckBox,会在LogCat中打印CheckBox当中text属性的文本内容,即“篮球”。

[b]二、RadioGroup和RadioButton[/b]
1.RadioButton:
RadioButton和CheckBox的区别:
a.单个RadioButton在选中后,通过点击无法变为未选中

单个CheckBox在选中后,通过点击可以变为未选中

b.一组RadioButton,只能同时选中一个

一组CheckBox,能同时选中多个

c.RadioButton在大部分UI框架中默认都以圆形表示

CheckBox在大部分UI框架中默认都以矩形表示

RadioButton和RadioGroup的关系:

a.RadioButton表示单个圆形单选框,而RadioGroup是可以容纳多个RadioButton的容器

b.每个RadioGroup中的RadioButton同时只能有一个被选中

c.不同的RadioGroup中的RadioButton互不相干,即如果组A中有一个选中了,组B中依然可以有一个被选中

d.大部分场合下,一个RadioGroup中至少有2个RadioButton

e.大部分场合下,一个RadioGroup中的RadioButton默认会有一个被选中,并建议您将它放在RadioGroup中的起始位置

2.RadioGroup:
RadioButton的一个集合,提供多选一的机制
3.属性:
android:orientation=”vertival” 垂直排布/”horizontal”水平排布
决定当前RadioGroup中RadioButton以什么形式排布
4.使用样例
a.布局文件activity_main.xml代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<RadioGroup
android:orientation="vertical"
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/nan" />

<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/nv" />

</RadioGroup>

</LinearLayout>

b.MainActivity java代码
public class MainActivity extends ActionBarActivity implements OnCheckedChangeListener {
private RadioGroup rg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rg=(RadioGroup) findViewById(R.id.radioGroup1);
//实现监听事件
rg.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(RadioGroup group, int checkId) {
switch (checkId) {
case R.id.radio0:
Log.i("tag","选择了男");
break;
case R.id.radio1:
Log.i("tag","选择了女");
break;
default:
break;
}
}
}

c.方法总结
setOnCheckedChangeListener RadioGroup的监听事件方法,本例是通过实现OnCheckedChangeListener 接口的方式来实现事件监听的。
onCheckedChanged方法中通过checkId去判断点击了哪个CheckButton。
d.运行结果
将程序不知道虚拟机上后,点击不同的RadioButton会在Logcat中打印相应的信息。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值