Android控制項之CheckBox、RadioButton用法執行個體分析_Android

來源:互聯網
上載者:User

本文執行個體講述了Android控制項之CheckBox、RadioButton用法。分享給大家供大家參考。具體如下:

CheckBox和RadioButton控制項都只有選中和未選中狀態,不同的是RadioButton是選項按鈕,需要編製到一個RadioGroup中,同一時刻一個RadioGroup中只能有一個按鈕處於選中狀態。

以下為CheckBox和RadioButton常用方法及說明

以下為選項按鈕和複選按鈕的使用方法

目錄結構:

main.xml布局檔案:

<?xml version="1.0" encoding="utf-8"?><ScrollView xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="fill_parent"  android:layout_height="fill_parent"  android:scrollbars="vertical">  <LinearLayout android:orientation="vertical"  android:layout_width="fill_parent"  android:layout_height="fill_parent">    <!-- RadioButton控制項示範 -->    <ImageView android:id="@+id/imageView01"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:src="@drawable/bulb_on"       android:layout_gravity="center_horizontal" />    <RadioGroup android:id="@+id/radioGroup"      android:orientation="horizontal"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:layout_gravity="center_horizontal">      <RadioButton android:id="@+id/on"        android:text="開燈"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:checked="true" />      <RadioButton android:id="@+id/off"        android:text="關燈"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />    </RadioGroup>    <!-- CheckBox控制項示範 -->    <ImageView android:id="@+id/imageView02"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:src="@drawable/bulb_on"       android:layout_gravity="center_horizontal" />    <CheckBox android:id="@+id/checkBox"      android:text="開燈"      android:checked="true"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:layout_gravity="center_horizontal" />  </LinearLayout></ScrollView>

CbRbActivity類:

package com.ljq.activity;import android.app.Activity;import android.os.Bundle;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.ImageView;import android.widget.RadioButton;import android.widget.CompoundButton.OnCheckedChangeListener;public class CbRbActivity extends Activity {  private ImageView imageView01=null;  private ImageView imageView02=null;  private CheckBox checkBox=null;  private RadioButton on=null;//開燈  @Override  public void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.main);    imageView01=(ImageView)findViewById(R.id.imageView01);    imageView02=(ImageView)findViewById(R.id.imageView02);    checkBox=(CheckBox)findViewById(R.id.checkBox);    on=(RadioButton)findViewById(R.id.on);    on.setOnCheckedChangeListener(listener);    checkBox.setOnCheckedChangeListener(listener);  }  OnCheckedChangeListener listener=new OnCheckedChangeListener(){    public void onCheckedChanged(CompoundButton buttonView,        boolean isChecked) {      if(buttonView instanceof RadioButton){        imageView01.setImageResource(isChecked?R.drawable.bulb_on:R.drawable.bulb_off);      }else if(buttonView instanceof CheckBox){        checkBox.setText(isChecked?"開燈":"關燈");        imageView02.setImageResource(isChecked?R.drawable.bulb_on:R.drawable.bulb_off);      }    }  };}

運行結果:

希望本文所述對大家的Android程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.