Android控件之CheckBox、RadioButton探究

CheckBox和RadioButton控件都只有选中和未选中状态,不同的是RadioButton是单选按钮,需要编制到一个RadioGroup中,同一时刻一个RadioGroup中只能有一个按钮处于选中状态。

以下为CheckBox和RadioButton常用方法及说明

2.png

2011-5-14 08:43:57 上传
下载附件 (7.59 KB)

以下为单选按钮和复选按钮的使用方法

目录结构

1.png

2011-5-14 08:43:56 上传
下载附件 (10.8 KB)

main.xml布局文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="fill_parent"
  4.     android:layout_height="fill_parent"
  5.     android:scrollbars="vertical">
  6.     <LinearLayout android:orientation="vertical"
  7.     android:layout_width="fill_parent"
  8.     android:layout_height="fill_parent">
  9.         <!-- RadioButton控件演示 -->
  10.         <ImageView android:id="@+id/imageView01"
  11.             android:layout_width="wrap_content"
  12.             android:layout_height="wrap_content"
  13.             android:src="@drawable/bulb_on"
  14.             android:layout_gravity="center_horizontal" />
  15.         <RadioGroup android:id="@+id/radioGroup"
  16.             android:orientation="horizontal"
  17.             android:layout_width="wrap_content"
  18.             android:layout_height="wrap_content"
  19.             android:layout_gravity="center_horizontal">
  20.             <RadioButton android:id="@+id/on"
  21.                 android:text="开灯"
  22.                 android:layout_width="wrap_content"
  23.                 android:layout_height="wrap_content"
  24.                 android:checked="true" />
  25.             <RadioButton android:id="@+id/off"
  26.                 android:text="关灯"
  27.                 android:layout_width="wrap_content"
  28.                 android:layout_height="wrap_content" />
  29.         </RadioGroup>
  30.         
  31.         <!-- CheckBox控件演示 -->
  32.         <ImageView android:id="@+id/imageView02"
  33.             android:layout_width="wrap_content"
  34.             android:layout_height="wrap_content"
  35.             android:src="@drawable/bulb_on"
  36.             android:layout_gravity="center_horizontal" />
  37.         <CheckBox android:id="@+id/checkBox"
  38.             android:text="开灯"
  39.             android:checked="true"
  40.             android:layout_width="wrap_content"
  41.             android:layout_height="wrap_content"
  42.             android:layout_gravity="center_horizontal" />
  43.     </LinearLayout>
  44. </ScrollView>
复制代码



CbRbActivity类

  1. package com.ljq.activity;

  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.widget.CheckBox;
  5. import android.widget.CompoundButton;
  6. import android.widget.ImageView;
  7. import android.widget.RadioButton;
  8. import android.widget.CompoundButton.OnCheckedChangeListener;

  9. public class CbRbActivity extends Activity {
  10.     private ImageView imageView01=null;
  11.     private ImageView imageView02=null;
  12.     private CheckBox checkBox=null;
  13.     private RadioButton on=null;//开灯
  14.    
  15.     @Override
  16.     public void onCreate(Bundle savedInstanceState) {
  17.         super.onCreate(savedInstanceState);
  18.         setContentView(R.layout.main);
  19.         
  20.         imageView01=(ImageView)findViewById(R.id.imageView01);
  21.         imageView02=(ImageView)findViewById(R.id.imageView02);
  22.         checkBox=(CheckBox)findViewById(R.id.checkBox);
  23.         on=(RadioButton)findViewById(R.id.on);
  24.         
  25.         on.setOnCheckedChangeListener(listener);
  26.         checkBox.setOnCheckedChangeListener(listener);
  27.     }
  28.    
  29.     OnCheckedChangeListener listener=new OnCheckedChangeListener(){

  30.         public void onCheckedChanged(CompoundButton buttonView,
  31.                 boolean isChecked) {
  32.             if(buttonView instanceof RadioButton){
  33.                 imageView01.setImageResource(isChecked?R.drawable.bulb_on:R.drawable.bulb_off);
  34.             }else if(buttonView instanceof CheckBox){
  35.                 checkBox.setText(isChecked?"开灯":"关灯");
  36.                 imageView02.setImageResource(isChecked?R.drawable.bulb_on:R.drawable.bulb_off);
  37.             }
  38.         }
  39.     };
  40. }
复制代码



运行结果

3.png

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值