您的位置:首页 > 其它

常用控件 05 单选按钮 Radio Buttons

2012-07-23 13:20 274 查看
Radio Buttons 只能从一组选项中选取一个。

创建
RadioButton


需要先创建一个
RadioGroup
,在内部声明
RadioButton
。如:

<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton android:id="@+id/radio_pirates"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pirates"
android:onClick="onRadioButtonClicked"/>
<RadioButton android:id="@+id/radio_ninjas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ninjas"
android:onClick="onRadioButtonClicked"/>
</RadioGroup>


然后在对应的Activity中处理该事件:

public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = (RadioButton) view).isChecked();

// Check which radio button was clicked
switch(view.getId()) {
case R.id.radio_pirates:
if (checked)
// Pirates are the best
break;
case R.id.radio_ninjas:
if (checked)
// Ninjas rule
break;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: