RadioButton为单选按钮,在一个RadioGroup中只能有一个按钮处于选中状态。CheckBox为多选按钮,可以选择多项
CheckBox与RadioButton常用方法以及说明
isCheck() 判断是否选中,如果选中返回true,否则返回false
performClick() 调用OnClickListener监听器,即模拟一次单击
setChecked() 通过传入的参数设置控件的状态
toggle() 置反控件当前的状态
setOnCheckedListener(CompoundButton.onCheckedChangeListener listener) 为控件设置监听器
1.strings.xml
<resources>
<string name="app_name">TestActivity</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
<string name="on">开灯</string>
<string name="off">关灯</string>
</resources>
2.main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/black" >
<ImageView
android:id="@+id/imageView01"
android:src="@drawable/bulb_off"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"/>
<RadioGroup
android:id="@+id/RadioGroup01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/off"
android:text="@string/off"
android:checked="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<RadioButton
android:id="@+id/on"
android:text="@string/on"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RadioGroup>
<CheckBox
android:id="@+id/CheckBox01"
android:text="@string/on"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
3.Activity代码
package com.example.testactivity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.app.Activity;
public class MainActivity extends Activity {
RadioButton buttonOff=null;
RadioButton buttonOn=null;
CheckBox box=null;
ImageView imageView=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
box=(CheckBox) findViewById(R.id.CheckBox01);
buttonOff=(RadioButton) findViewById(R.id.off);
box.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
setBulbState(isChecked);
}
});
buttonOff.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
setBulbState(!isChecked);
}
});
}
public void setBulbState(Boolean state){
ImageView iv=(ImageView) findViewById(R.id.imageView01);
iv.setImageResource((state)?R.drawable.bulb_on:R.drawable.bulb_off);
box=(CheckBox) findViewById(R.id.CheckBox01);
box.setText(((state)?R.string.off:R.string.on));
box.setChecked(state);
buttonOff=(RadioButton) findViewById(R.id.off);
buttonOff.setChecked(!state);
buttonOn=(RadioButton) findViewById(R.id.on);
buttonOn.setChecked(state);
}
}