3.2 Radio and switch

Source: Internet
Author: User

Same point:

RadioButton (radio button), CheckBox (check button), ToggleButton (switch button) are inherited from Android.widget.CompoundButton class, and Compoundbutton inherits from the Button class, in which a checked attribute is encapsulated to determine whether it is selected, which is also the same as the button, which is expanded to use the same property in the three controls.

The general checked property is set and obtained in the following ways:

    • Android:checked/setchecked (Boolean): setting is selected.
    • IsChecked (): Gets whether it is selected.

Different points:

1.RadioButton: Radio button,2 or more than 2 radio boxes, generally with radiogroup, in the same radiogroup, all of the RadioButton is selected as mutually exclusive, They have and only one RadioButton are selected, but they do not affect each other in different radiogroup.

2.Switch: Switch (NEW: On Android 4.0 (API level 14)), the equivalent of a TextView plus a whether the radio box (a text display + Switch button), there are two states , probably the use of the above two controls have been, When you can display different states by two properties, the contents of the displayed text in the control are different, with the following properties:

    • Android:textoff/settextoff (charsequence): Sets the content to be displayed when it is closed.
    • Android:texton/settexton (charsequence): Sets the content to be displayed when open.

There is a Oncheckedchangelistener () event that is triggered when the switch's state is switched, which needs to be passed within the implementation of a Oncheckedchangelistener interface, and when it is switched, Triggers the Oncheckedchange () method in which the function code that needs to be implemented is written.

3.ToggleButton: Switch (old:: Android 4.0 (API level 14)), equivalent to whether the radio box, the same as 2

Interface:

Layout:

<?XML version= "1.0" encoding= "Utf-8"?><Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context= "Com.liang.radioswitchtoggle.MainActivity">    <TextViewAndroid:id= "@+id/textview"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignparentstart= "true"Android:layout_alignparenttop= "true"Android:text= "Sex:"android:textappearance= "? Android:attr/textappearancelarge" />    <RadiogroupAndroid:id= "@+id/radiogroup"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:layout_alignbottom= "@+id/textview"Android:layout_marginstart= "16DP"android:layout_toendof= "@+id/textview"android:orientation= "Horizontal">        <RadioButtonAndroid:id= "@+id/rbmale"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:checked= "false"Android:onclick= "Radioclick"Android:text= "Male" />        <RadioButtonAndroid:id= "@+id/rbfemale"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:checked= "false"Android:onclick= "Radioclick"Android:text= "female" />        <RadioButtonAndroid:id= "@+id/rbsecret"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:checked= "false"Android:onclick= "Radioclick"Android:text= "Secret" />    </Radiogroup>    <SwitchAndroid:id= "@+id/switch1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignparentstart= "true"Android:layout_below= "@+id/textview"Android:layout_margintop= "60DP"android:checked= "false"Android:text= "New Switch"Android:textoff= "Off"Android:texton= "Open" />    <!--android:onclick= "Switchclick" android:checked= "false"/> -    <ToggleButtonAndroid:id= "@+id/togglebutton"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignparentstart= "true"Android:layout_below= "@+id/switch1"Android:layout_margintop= "53DP"Android:onclick= "Toggleclick"Android:text= "Old switch"Android:textoff= "Off"Android:texton= "Open" /></Relativelayout>

Code:

Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.CompoundButton;ImportAndroid.widget.RadioButton;ImportAndroid.widget.Switch;ImportAndroid.widget.Toast;ImportAndroid.widget.ToggleButton; Public classMainactivityextendsappcompatactivity {PrivateSwitch Aswitch; PrivateToggleButton ToggleButton; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);       Setcontentview (R.layout.activity_main); //method One listeningAswitch = (Switch) This. Findviewbyid (R.ID.SWITCH1); Aswitch.setoncheckedchangelistener ( New Compoundbutton.oncheckedchangelistener () {@Override public void oncheckedchanged (COMPOUNDBU Tton Buttonview, Boolean isChecked) {//Buttonview.gettext () the text in front of the switch is not on/off                if(isChecked) {toast.maketext (mainactivity. This, Buttonview.gettext () + "is" +aswitch.gettexton (). toString (), 1). Show (); } Else{toast.maketext (mainactivity). This, Buttonview.gettext () + "is" +ischecked, 1). Show ();        }            }        }); /*togglebutton= (ToggleButton) This.findviewbyid (R.id.togglebutton);            Togglebutton.setoncheckedchangelistener (New Compoundbutton.oncheckedchangelistener () {@Override                    public void OnCheckedChanged (Compoundbutton buttonview, Boolean isChecked) { if (isChecked) {                Toast.maketext (Mainactivity.this, Buttonview.gettext () + "is" +ischecked, 1). Show ();                } else {Toast.maketext (mainactivity.this, Buttonview.gettext () + "is" +ischecked, 1). Show (); }            }        });*/    }    //need a strong turn     Public voidRadioclick (view view) { RadioButton RB = (RadioButton) view; BooleanisChecked =rb.ischecked (); Switch(View.getid ()) { CaseR.id.rbmale:if(isChecked) {toast.maketext (mainactivity. This, Rb.gettext () + "true", 1). Show (); } Else {                    //the case of a single radio without falseToast.maketext (mainactivity. This, "No false case", 1). Show (); }                 Break;  CaseR.id.rbfemale:if(isChecked) {toast.maketext (mainactivity. This, "Female true", 1). Show (); }                 Break;  CaseR.id.rbsecret:toast.maketext (mainactivity. This, "secret" + isChecked, 1). Show ();  Break; }    }    //method Two The onclick of XML/*Public void Switchclick (view view) { switch s = (switch) view;        Boolean ischecked = s.ischecked ();        if (ischecked) {Toast.maketext (Mainactivity.this, S.gettext () + "is" + ischecked, 1). Show ();        } else {Toast.maketext (mainactivity.this, S.gettextoff (). toString () + "is" + ischecked, 1). Show (); }    }*/     Public voidToggleclick (view view) { ToggleButton t = (ToggleButton) view; Booleanischecked =t.ischecked (); //Gettexton () or Gettextoff (). toString () and GetText () values        if(ischecked) {toast.maketext (mainactivity. This, T.gettexton (). toString () + "is" + ischecked, 1). Show (); } Else{toast.maketext (mainactivity). This, T.gettext () + "is" + ischecked, 1). Show (); }    }}

3.2 Radio and switch

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.