官方对RadioButton的说明是这样的: RadioButton有checked 和unchecked两种状态 。 当RadioButton的状态是unchecked时,用户可以press或click使其checked,但是不能通过再次press或click该RadioButton使其unchecked。RadioButton通常都是和RadioGroup结合使用——在一个RadioGroup中最多只有一个RadioButton被选中。
另外,自定义RadioButton的方法和CheckBox一样,详细方法可参见一步一步学android控件(之十六)—— CheckBox
RadioButton的内容比较简单,下面直接看代码:
1、布局文件widget_radio_layout.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="vertical" >
<RadioButton
android:id="@+id/radio_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/str_for_radio_btn_one" />
<RadioButton
android:id="@+id/radio_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="@string/str_for_radio_btn_two" />
<RadioGroup
android:id="@+id/radio_group_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/button_drawable_normal_start" >
<RadioButton
android:id="@+id/radio_group_btn_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/str_group_radio_btn_one" />
<RadioButton
android:id="@+id/radio_group_btn_two"
style="@style/check_box_theme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/str_group_radio_btn_two" />
<RadioButton
android:id="@+id/radio_group_btn_three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/str_group_radio_btn_three" />
</RadioGroup>
</LinearLayout>
RadioGroup管理其内部的RadioButton状态,这里包含了三个RadioButton。单个的RadioButton只能从unchecked状态到checked状态。
2、使用到的字符串strings.xml
<!-- strings for radioButton & radioGroup -->
<string name="str_for_radio_btn_one">RadioButton one</string>
<string name="str_for_radio_btn_two">RadioButton two</string>
<string name="str_group_radio_btn_one">Group RadioButton one</string>
<string name="str_group_radio_btn_two">Group RadioButton two</string>
<string name="str_group_radio_btn_three">Group RadioButton three</string>
<!-- end -->
3、activity——WidgetRadioActivity.java
package com.xy.zt.selfdefinewieget;
import android.app.Activity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class WidgetRadioActivity extends Activity implements
OnCheckedChangeListener, RadioGroup.OnCheckedChangeListener {
private RadioButton mBtnOne;
private RadioButton mBtnTwo;
private RadioButton mBtnGroupOne;
private RadioButton mBtnGroupTwo;
private RadioButton mBtnGroupThree;
private RadioGroup mRadioGroup;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.widget_radio_layout);
init();
}
private void init() {
mBtnOne = (RadioButton) findViewById(R.id.radio_one);
mBtnTwo = (RadioButton) findViewById(R.id.radio_two);
mBtnOne.setOnCheckedChangeListener(this);
mBtnTwo.setOnCheckedChangeListener(this);
mBtnGroupOne = (RadioButton) findViewById(R.id.radio_group_btn_one);
mBtnGroupTwo = (RadioButton) findViewById(R.id.radio_group_btn_two);
mBtnGroupThree = (RadioButton) findViewById(R.id.radio_group_btn_three);
mRadioGroup = (RadioGroup) findViewById(R.id.radio_group_group);
mRadioGroup.setOnCheckedChangeListener(this);
}
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(
this,
"Single RadioButton state can only from unchecked to checked !...",
Toast.LENGTH_LONG).show();
}
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.radio_group_btn_one:
Toast.makeText(this, "Radio one is checked !...", Toast.LENGTH_LONG)
.show();
break;
case R.id.radio_group_btn_two:
Toast.makeText(this, "Radio two is checked !...", Toast.LENGTH_LONG)
.show();
break;
case R.id.radio_group_btn_three:
Toast.makeText(this, "Radio three is checked !...",
Toast.LENGTH_LONG).show();
break;
}
}
}
RadioButton & RadioRgoup 就学完了,下一篇学习ToggleButton & Switch。