RadioButton是Button中的一种,主要在开发中用于布局,可以让用户进行单项或者多项选择
package com.activity;
import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class RadioActivity extends Activity {
/** Called when the activity is first created. */
private RadioGroup radioGroup;
private RadioButton maleButton;
private RadioButton femaleButton;
private CheckBox swimBox;
private CheckBox runBox;
private CheckBox ballBox;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//根据id获取控件对象
radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
maleButton = (RadioButton) findViewById(R.id.maleButton);
femaleButton = (RadioButton) findViewById(R.id.femaleButton);
swimBox = (CheckBox) findViewById(R.id.swim);
runBox = (CheckBox) findViewById(R.id.run);
ballBox = (CheckBox) findViewById(R.id.ball);
//匿名内部类
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
//当用户点击单选按钮时,会立刻触发该事件
//会传入两个参数,一个是所在组,一个是组里面所点击的单选按钮id
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
//判断用户所点击的是哪一个
if(maleButton.getId() == checkedId){
Toast.makeText(RadioActivity.this, "male", Toast.LENGTH_SHORT).show();
}else if(femaleButton.getId() == checkedId){
Toast.makeText(RadioActivity.this, "female", Toast.LENGTH_SHORT).show();
}
}
});
//为每一个复选按钮增加监听
swimBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
Toast.makeText(RadioActivity.this, "swim is checked", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(RadioActivity.this, "swim is unchecked", Toast.LENGTH_SHORT).show();
}
}
});
runBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
Toast.makeText(RadioActivity.this, "run is checked", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(RadioActivity.this, "run is unchecked", Toast.LENGTH_SHORT).show();
}
}
});
ballBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
Toast.makeText(RadioActivity.this, "ball is checked", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(RadioActivity.this, "ball is unchecked", Toast.LENGTH_SHORT).show();
}
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<RadioButton
android:id="@+id/maleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/male"
/>
<RadioButton
android:id="@+id/femaleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/female"
/>
</RadioGroup>
<CheckBox
android:id="@+id/swim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/swim"
/>
<CheckBox
android:id="@+id/run"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/run"
/>
<CheckBox
android:id="@+id/ball"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ball"
/>
</LinearLayout>