布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".RadioTest" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<RadioGroup android:id="@+id/genderGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<RadioButton
android:id="@+id/womanbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/woman"/>
<RadioButton
android:id="@+id/manbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/man"/>
</RadioGroup>
<CheckBox
android:id="@+id/seven"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/seven"/>
<CheckBox
android:id="@+id/lmz"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/lmz"/>
<CheckBox
android:id="@+id/love"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/love"/>
</LinearLayout>
java 文件
package seven.activity06;
import android.os.Bundle;
import android.app.Activity;
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 RadioTest extends Activity {
// 对控件对像进行声明
private RadioGroup genderGroup = null;
private RadioButton manbutton = null;
private RadioButton womanbutton = null;
private CheckBox seven = null;
private CheckBox lmz = null;
private CheckBox love = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 通过控件的ID 来得到代表控件的对象
genderGroup = (RadioGroup)findViewById(R.id.genderGroup);
womanbutton = (RadioButton)findViewById(R.id.womanbutton);
manbutton = (RadioButton)findViewById(R.id.manbutton);
seven = (CheckBox)findViewById(R.id.seven);
lmz = (CheckBox)findViewById(R.id.lmz);
love = (CheckBox)findViewById(R.id.love);
// 为 radiogroup 设置监听器。 需要注意的是。这里的监听器有所不同。
// 是在该按钮的状态被改变以后调用该函数。
genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if(womanbutton.getId() == checkedId)
{
System.out.println("woman");
Toast.makeText(RadioTest.this, "woman", Toast.LENGTH_SHORT).show();
}
else if(manbutton.getId() == checkedId)
{
Toast.makeText(RadioTest.this, "man", Toast.LENGTH_SHORT).show();
}
}
});
// 为多选按钮添加监听器
seven.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked)
{
Toast.makeText(RadioTest.this, "已选择", Toast.LENGTH_SHORT).show();
}
}
// 接下来如上,的监听如上 不再写下去
});
}
}