一:RadioButton的相关属性:
1.Activity
//单选按钮
public class RadioButtonActivity extends Activity { private Context context;
private RadioGroup sexRadioGroup;
private RadioButton maleRadioButton;
private RadioButton femaleRadioButton;
private RadioButton sexRadioButton;
private Button submitButton; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.radio_button); init();
addAction(); } private void init() {
context = this;
sexRadioGroup = (RadioGroup) findViewById(R.id.sexRadioGroupId);
maleRadioButton = (RadioButton) findViewById(R.id.maleRadioButtonId);
femaleRadioButton = (RadioButton) findViewById(R.id.femaleRadioButtonId);
sexRadioButton = (RadioButton) findViewById(R.id.sexRadioButtonId);
submitButton = (Button) findViewById(R.id.submitButtonId);
} private void addAction() {
// sexRadioGroup.setOnClickListener(l),setOnClickListener:点击的时候触发
// setOnCheckedChangeListener:状态改变的时候触发
// 两者都能实现对CheckBox的状态改变的监听,但一般情况下,用的更多的是setOnCheckedChangeListener。
//因为,当CheckBox的状态不是通过点击事件改变,而是通过其他的方式改变时,比如setCheck(),setOnClickListener无法完成此种情况下的监听。
//OnCheckChangedListener监听CheckBox的状态,无论来自你的onClick事件还是其他。
sexRadioGroup
.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.maleRadioButtonId:
Toast.makeText(context, "你选择了\"男\".",
Toast.LENGTH_SHORT).show();
break;
case R.id.femaleRadioButtonId:
Toast.makeText(context, "你选择了\"女\".",
Toast.LENGTH_SHORT).show();
break;
case R.id.sexRadioButtonId:
Toast.makeText(context, "你选择了\"人妖\".",
Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}); submitButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String sex = null;
if (maleRadioButton.isChecked()) {
sex = "男";
}
if (femaleRadioButton.isChecked()) {
sex = "女";
}
if (sexRadioButton.isChecked()) {
sex = "人妖";
}
Toast.makeText(context, "你的性别是:" + sex, Toast.LENGTH_SHORT)
.show();
}
});
} }
2.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"
android:padding="5dp" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性别:"
android:textSize="20sp" /> <RadioGroup
android:id="@+id/sexRadioGroupId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<!-- android:button="@null" 可以自定义图片-->
<RadioButton
android:id="@+id/maleRadioButtonId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="男" /> <RadioButton
android:id="@+id/femaleRadioButtonId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女" /> <RadioButton
android:id="@+id/sexRadioButtonId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="人妖" />
</RadioGroup>
</LinearLayout> <Button
android:id="@+id/submitButtonId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="确定"
android:textSize="20sp" /> </LinearLayout>
3.效果图展示: