Android Beginner tutorial Small case of single box radiogroup with check box checkbox

Source: Internet
Author: User

Android inside the radio box and HTML is actually the same effect. Here are two controls: CheckBox and Radiogroup. Directly on the code:

Radio.xml Layout file:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/    Android "android:orientation=" vertical "android:layout_width=" fill_parent "android:layout_height=" Fill_parent " ><textviewandroid:id= "@+id/textview1" android:layout_width= "fill_parent" android:layout_height= "Wrap_conte NT "android:text=" @string/hello "/><radiogroupandroid:id=" @+id/gendergroup "android:layout_width=" Wrap_con Tent "android:layout_height=" wrap_content "android:orientation=" vertical "> <radiobutton android:id = "@+id/femalebutton" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:text       = "@string/female"/> <radiobutton android:id= "@+id/malebutton" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "@string/male"/></radiogroup><checkboxandroi D:id= "@+id/swim" Android:layout_widTh= "Wrap_content" android:layout_height= "wrap_content" android:text= "@string/swim"/><checkboxandroid:id= "@ +id/run "android:layout_width=" wrap_content "android:layout_height=" wrap_content "android:text=" @string/run "/>   <checkboxandroid:id= "@+id/read" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:text= "@string/read"/></linearlayout>


String.xml Code:

<?xml version= "1.0" encoding= "Utf-8"?><resources> <string    name= "Hello" >hello world, activity07!</string>    <string name= "app_name" >activity07</string>    <string name= "male "> Male </string>    <string name=" female "> Female </string>    <string name=" Swim ">swim</ string>    <string name= "Run" >run</string>    <string name= "read" >read</string>< /resources>
radiotest:

Package Mars.activity07;import Android.app.activity;import Android.os.bundle;import android.widget.checkbox;import Android.widget.compoundbutton;import Android.widget.radiobutton;import Android.widget.radiogroup;import Android.widget.toast;public class Radiotest extends activity {/** Called when the activity is first created. *///to control objects DECLARE private Radiogroup Gendergroup = null;private RadioButton Femalebutton = null;private RadioButton Malebutton = NULL;PR    ivate checkbox swimbox = null;private checkbox runbox = null;private checkbox readbox = null;        @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.radio);        The Object gendergroup = (radiogroup) Findviewbyid (R.id.gendergroup) that represents the control is obtained through the control's ID.        Femalebutton = (RadioButton) Findviewbyid (R.id.femalebutton);        Malebutton = (RadioButton) Findviewbyid (R.id.malebutton);        Swimbox = (CheckBox) Findviewbyid (r.id.swim); Runbox = (ChEckbox) Findviewbyid (R.id.run);        Readbox = (CheckBox) Findviewbyid (R.id.read); To set the listener for Radiogroup, note that the Listener and the button control's listener are different gendergroup.setoncheckedchangelistener (new Radiogroup.oncheckedchangelistener () {@Overridepublic void oncheckedchanged (radiogroup group, int checkedid) {//TODO Auto-generated Method Stubif (femalebutton.getid () = = Checkedid) {System.out.println ("Famale"); Toast.maketext (Radiotest.this, "Famle", Toast.length_short). Show (); else if (malebutton.getid () = = Checkedid) {System.out.println ("male");                Toast.maketext (Radiotest.this, "male", Toast.length_short). Show ();}});  Add Listener Swimbox.setoncheckedchangelistener (new Compoundbutton.oncheckedchangelistener () {@Overridepublic void for the multi-select button OnCheckedChanged (Compoundbutton Buttonview, Boolean isChecked) {//TODO auto-generated method stubif (isChecked)// Unchecked to the selected state is performed here: {System.out.println ("swim is checked");}        else//is executed here by the selected state to the unchecked state: {System.out.println ("Swim is Unchecked");}}); runbox.seToncheckedchangelistener (New Compoundbutton.oncheckedchangelistener () {@Overridepublic void oncheckedchanged ( Compoundbutton Buttonview, Boolean isChecked) {//TODO auto-generated method stubif (isChecked) {System.out.println ("Run is checked ");}        Else{system.out.println ("Run is Unchecked");}}); Readbox.setoncheckedchangelistener (New Compoundbutton.oncheckedchangelistener () {@Overridepublic void OnCheckedChanged (Compoundbutton Buttonview, Boolean isChecked) {//TODO auto-generated method stubif (isChecked) { System.out.println ("read is checked");}    Else{system.out.println ("read is unchecked");}}); }    }


Registration file for registration:

<application android:icon= "@drawable/icon" android:label= "@string/app_name" >        <activity android:name= ". Radiotest "                  android:label=" @string/app_name ">            <intent-filter>                <action android:name=" Android.intent.action.MAIN "/>                <category android:name=" Android.intent.category.LAUNCHER "/>            < /intent-filter>        </activity>    </application>

Perform:



Android Beginner tutorial Small case of single box radiogroup with check box checkbox

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.