Use of Android checkbox and RadioButton as well as toast and alertdialog

Source: Internet
Author: User

Package Com.example.radiobutton_01;import Android.app.activity;import Android.os.bundle;import Android.view.View; Import Android.widget.button;import Android.widget.radiobutton;import Android.widget.radiogroup;import    Android.widget.toast;public class MyActivity extends Activity {private Radiogroup RG;    Private Button btn;     /** * Called when the activity is first created.        */@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.main);        RG = (Radiogroup) Findviewbyid (R.ID.RG);        BTN = (Button) Findviewbyid (R.ID.BTN);                Btn.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (view view) {                int len = Rg.getchildcount ();                String msg = "";                    for (int i=0;i<len;i++) {RadioButton RB = (RadioButton) rg.getchildat (i);                   if (rb.ischecked ()) {     msg = Rb.gettext (). toString ();                    Break            }} toast.maketext (Myactivity.this,msg,toast.length_short). Show ();    }        }); }}

Package Com.example.checkbox_01;import Android.app.activity;import Android.app.alertdialog;import Android.os.bundle;import Android.view.view;import Android.widget.button;import Android.widget.CheckBox;import Android.widget.linearlayout;import Java.util.arraylist;import Java.util.list;public class MyActivity extends    Activity implements view.onclicklistener{private list<checkbox> checkboxes = new arraylist<checkbox> ();     /** * Called when the activity is first created.        */@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.main);        string[] Checkboxtext = new string[]{"Is you student?", "Is Your Love Android?", "Is You dev?"};        LinearLayout linearlayout = (linearlayout) getlayoutinflater (). Inflate (R.layout.main,null); for (int i=0;i<checkboxtext.length;i++) {checkbox checkbox = (CheckBox) getlayoutinflater (). Inflate (r.layout.Checkbox,null);            Checkbox.settext (Checkboxtext[i]);            Checkboxes.add (CheckBox);        Linearlayout.addview (Checkbox,i);        } setcontentview (LinearLayout);        Button btn = (button) Findviewbyid (R.ID.BTN);    Btn.setonclicklistener (this);        } @Override public void OnClick (view view) {String s = "";            for (CheckBox cb:checkboxes) {if (cb.ischecked ()) {S+=cb.gettext () + "\ n";        }} if ("". Equals (s)) {s = "You did not select the option";    } new Alertdialog.builder (this). Setmessage (s). Setpositivebutton ("Off", null). Show (); }}

Use of Android checkbox and RadioButton as well as toast and alertdialog

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.