CheckBox与RadioButton的使用

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);
     }
     
 }
 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值