1.在strings.xml中添加字符串
<resources>
<string name="app_name">RadioCheck</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
<string name="male">男</string>
<string name="famle">女</string>
<string name="red">红色</string>
<string name="black">黑色</string>
<string name="blue">蓝色</string>
</resources>
2.在布局文件中添加控件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="@+id/male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/male"/>
<RadioButton
android:id="@+id/famle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/famle"/>
</RadioGroup>
<CheckBox
android:id="@+id/red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/red"/>
<CheckBox
android:id="@+id/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/black"/>
<CheckBox
android:id="@+id/blue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/blue"/>
</LinearLayout>
3完整代码
package com.example.radiocheck;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends Activity {
private RadioGroup radioGroup;
private RadioButton maleButton;
private RadioButton famleButton;
private CheckBox redBox ;
private CheckBox blackBox;
private CheckBox blueBox;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//通过控件的ID来得到代表控件的对象
radioGroup = (RadioGroup)findViewById(R.id.radioGroup);
maleButton = (RadioButton)findViewById(R.id.male);
famleButton = (RadioButton)findViewById(R.id.famle);
blueBox = (CheckBox)findViewById(R.id.blue);
blackBox = (CheckBox)findViewById(R.id.black);
redBox = (CheckBox)findViewById(R.id.red);
//设置被选中的触发事件
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(maleButton.getId()==checkedId){
Toast.makeText(getApplicationContext(), "男",Toast.LENGTH_SHORT).show();
}else if(famleButton.getId()==checkedId)
{
Toast.makeText(getApplicationContext(), "女",Toast.LENGTH_SHORT).show();
}
}
});
blueBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
Toast.makeText(getApplicationContext(), "蓝色", Toast.LENGTH_SHORT).show();
}else
{
Toast.makeText(getApplicationContext(), "蓝色取消", Toast.LENGTH_SHORT).show();
}
}
});
redBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
Toast.makeText(getApplicationContext(), "红色", Toast.LENGTH_SHORT).show();
}else
{
Toast.makeText(getApplicationContext(), "红色取消", Toast.LENGTH_SHORT).show();
}
}
});
blackBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
Toast.makeText(getApplicationContext(), "黑色", Toast.LENGTH_SHORT).show();
}else
{
Toast.makeText(getApplicationContext(), "黑色取消", Toast.LENGTH_SHORT).show();
}
}
});
}
}
4.显示效果