Home>

The checkbox and radiobutton controls are only checked and unchecked.The difference is that radiobutton is a radio button,It needs to be compiled into a radiogroup. Only one button in a radiogroup can be selected at a time.

The following are common methods and instructions for checkbox and radiobutton

Here's how to use radio buttons and check buttons

Directory Structure:

main.xml layout file:

<?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">
    <!-Demo of radiobutton control->
    <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="ON light"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true" />
      <radiobutton android:id="@ + id/off"
        android:text="turn off the light"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    </radiogroup>
    <!-Checkbox control demo->
    <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="ON light"
      android:checked="true"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center_horizontal" />
  </linearlayout>
</scrollview>

cbrbactivity class:

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;//Turn on the light
  @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?"On Light":"Off Light");
        imageview02.setimageresource (ischecked?r.drawable.bulb_on:r.drawable.bulb_off);
      }
    }
  };
}

operation result:

  • Previous Analysis of functions and pointers in C ++
  • Next Talking about the difference between CMD and win powershell