Android Note (v): Radio in Android

Source: Internet
Author: User

Original address: http://irving-wei.iteye.com/blog/1076097

The checkbox is described in the previous section, which will be exposed to Radiogroup and RadioButton.

Their relationship is: a radiogroup corresponds to multiple RadioButton, and a RadioButton in a radiogroup can only be selected at the same time, and its selected value is the selected value of the Radiogroup.


The code for this section runs as follows:



The specific code is written as follows:
First, add the string you want to use for this program in Strings.xml:

XML code
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <resources>
  3. <string name="Hello">hello world, test! </string>
  4. <string name="app_name">androidwithradiogroup</string>
  5. <string name="Radio_1"> Handsome </string>
  6. <string name="radio_2"> Beauty </string>
  7. <string name="question"> May I ask your gender? </string>
  8. </Resources>



The next step is to add a textview and a radiogroup that shows the information in Main.xml, which contains two RadioButton, with the following code:

XML code
  1. <TextView
  2. android:id="@+id/showtext"
  3. android:layout_width="228px"
  4. android:layout_height="49px"
  5. android:text="@string/question"
  6. android:textsize="20sp"
  7. />
  8. <!--build a radiogroup --
  9. <Radiogroup
  10. android:id="@+id/radiogroup"
  11. android:layout_width="137px"
  12. android:layout_height="216px"
  13. android:orientation="Horizontal"
  14. >
  15. <!--Build a RadioButton --
  16. <RadioButton
  17. android:id="@+id/radiobutton1"
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:text="@string/radio_1"
  21. />
  22. <!--build a second RadioButton --
  23. <RadioButton
  24. android:id="@+id/radiobutton2"
  25. android:layout_width="wrap_content"
  26. android:layout_height="wrap_content"
  27. android:text="@string/radio_2"
  28. />
  29. </radiogroup>



The next step is to get the three components defined above in the subclass of activity, and then add the Oncheckedchangelistener to Radiogroup and implement the listening method.
The code is as follows:

Java code
    1. Super.oncreate (savedinstancestate);
    2. Setcontentview (R.layout.main);
    3. TextView = (TextView) Findviewbyid (R.id.showtext);
    4. Radiogroup = (radiogroup) Findviewbyid (R.id.radiogroup);
    5. RadioButton1 = (RadioButton) Findviewbyid (R.id.radiobutton1);
    6. RadioButton2 = (RadioButton) Findviewbyid (R.id.radiobutton2);
    7. Radiogroup.setoncheckedchangelistener (new Oncheckedchangelistener () {
    8. public void OnCheckedChanged (radiogroup group, int checkedid) {
    9. if (Checkedid = = Radiobutton1.getid ()) {
    10. Textview.settext (Radiobutton1.gettext ());
    11. } Else if (Checkedid = = Radiobutton2.getid ()) {
    12. Textview.settext (Radiobutton2.gettext ());
    13. }
    14. }
    15. });

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.