笔记摘要:
这里主要简单的介绍了RadioGroup中的RadioButton和CheckBox的创建方法和监听器的设置
需要注意的是:RadioGroup的监听器和Button控件的监听器有所不同,分别为:
RadioGroup.OnCheckedChangeListener
CompoundButton.OnCheckedChangeListener
示例图示:
radio.xml代码:采用RelativeLayout的布局
<RelativeLayout 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" >
<!-- TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world"
tools:context=".RatioActivity" -->
<TextView
android:text="@string/textView1"
android:id="@+id/textView1"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
/>
<RadioGroup
android:id="@+id/radioGroup"
android:layout_below="@id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/male"
android:text="@string/male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<RadioButton
android:id="@+id/female"
android:text="@string/female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RadioGroup>
<TextView
android:id="@+id/textView2"
android:text="@string/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/radioGroup"
/>
<CheckBox
android:id="@+id/football"
android:text="@string/football"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView2"
/>
<CheckBox
android:id="@+id/swim"
android:text="@string/swim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/football"
/>
<CheckBox
android:id="@+id/basketball"
android:text="@string/basketball"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/swim"/>
</RelativeLayout>
RadioActivityjava代码
package com.example.ratio;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.CompoundButton;
public class RatioActivity extends Activity {
private final String TAG = "RatioActivity" ;
private RadioGroup radioGroup = null;
private RadioButton femaleButton = null;
private RadioButton maleButton = null;
private CheckBox swimBox = null;
private CheckBox footballBox = null;
private CheckBox basketballBox = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ratio);
//获取各种组件对象
radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
femaleButton = (RadioButton) findViewById(R.id.female);
maleButton = (RadioButton) findViewById(R.id.male);
swimBox= (CheckBox) findViewById(R.id.swim);
footballBox= (CheckBox) findViewById(R.id.football);
basketballBox= (CheckBox) findViewById(R.id.basketball);
//为RadioGroup设置监听器,需要注意的是:这里的监听器和Button控件的监听器有所不同
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if(femaleButton.getId()==checkedId){
Log.i(TAG,"female");
Toast.makeText(RatioActivity.this, "female", Toast.LENGTH_LONG).show();
}
else if(maleButton.getId()==checkedId){
Log.i(TAG,"male");
Toast.makeText(RatioActivity.this, "male", Toast.LENGTH_LONG).show();
}
}
});
//为多选按钮添加监听器
swimBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked){
if(isChecked){
Log.i(TAG,"swim is checked");
}
else{
Log.i(TAG,"swim is unchecked");
}
}
});
basketballBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked){
if(isChecked){
Log.i(TAG,"basketball is checked");
}
else{
Log.i(TAG,"basketball is unchecked");
}
}
});
footballBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
Log.i(TAG,"football is checked");
}
else{
Log.i(TAG,"football is unchecked");
}
}
});
basketballBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
if(isChecked){
Log.i(TAG,"basketball is checked");
}
else{
Log.i(TAG,"basketball is unchecked");
}
}
});}}