Example Analysis of checkbox and RadioButton usage of Android control _android

Source: Internet
Author: User

This example describes the checkbox, RadioButton usage of the Android control. Share to everyone for your reference. Specifically as follows:

Both the checkbox and the RadioButton control are checked and unchecked, except that the RadioButton is a radio button that needs to be compiled into a radiogroup that only one button in a radiogroup can be selected at the same time.

The following are common methods and descriptions for checkbox and RadioButton

The following are the ways 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 > <!--RadioButton control demo--> <imageview android:id= "@+id/imageview01" android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:src= "@drawable/bulb_on" Android:layout_grav
      ity= "Center_horizontal"/> <radiogroup android:id= "@+id/radiogroup" android:orientation= "Horizontal" Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "Center_horiz" Ontal "> <radiobutton android:id=" @+id/on "android:text=" Turn on the Light "android:layout_width=" wrap_content "Android:layout_height=" Wrap_contEnt "android:checked=" true "/> <radiobutton android:id=" @+id/off "android:text=" Turn off Lights "an Droid:layout_width= "Wrap_content" android:layout_height= "wrap_content"/> </RadioGroup> <!--C Heckbox 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"/&gt
    ; <checkbox android:id= "@+id/checkbox" android:text= "Turn on the Lights" android:checked= "true" Android:layout_width= "W Rap_content "android:layout_height=" wrap_content "android:layout_gravity=" Center_horizontal "/> </Line

 Arlayout> </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;//lights @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (Savedi
    Nstancestate);
    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) {Imageview01.setimageresource (ischecked?)
      R.drawable.bulb_on:r.drawable.bulb_off); }else if (Buttonview instanceof CheckBox) {checkbox.settext (ischecked?)
        Turn on the Light ":" Turn off the Light "); Imageview02.setimageresource (ischecked?)
      R.drawable.bulb_on:r.drawable.bulb_off);
}
    }
  };

 }

Run Result:

I hope this article will help you with your Android program.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.