Android--oncheckedchangelistener instances

Source: Internet
Author: User

The. XML code is as follows:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "/http Schemas.android.com/apk/res/android "android:layout_width=" fill_parent "android:layout_height=" Fill_parent "Andro id:orientation= "vertical" ><textview android:id= "@+id/show" android:layout_width= "Fill_parent" Android:lay out_height= "Wrap_content" android:text= "Your gender is:" Android:textsize= "20px"/><radiogroup android:id= "@+id/    Sex "android:layout_width=" fill_parent "android:layout_height=" wrap_content "android:orientation=" Horizontal " android:checkedbutton= "@+id/male" > <radiobutton android:id= "@+id/male" android:layout_width= "Wrap_conte NT "android:layout_height=" wrap_content "android:text=" male "/> <radiobutton android:id=" @+id/female "and Roid:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "female"/> </RADIOGROUP>&L T;/linearlayout> 

. Java code is as follows:

Package Org.lxh.demo;import Android.app.activity;import Android.os.bundle;import android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.checkbox;import Android.widget.edittext;import Android.widget.radiobutton;import Android.widget.radiogroup;import Android.widget.radiogroup.oncheckedchangelistener;import Android.widget.textview;public class Hello extends Activity {private TextView text=null;private radiogroup sex=null;private RadioButton male=null;private RadioButton female=null;public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Life cycle Method Super.setcontentview (R.layout.main); Set the layout manager this.text= (TextView) Super.findviewbyid (r.id.show) to be used, this.sex= (Radiogroup) Super.findviewbyid ( R.id.sex); this.male= (RadioButton) Super.findviewbyid (R.id.male); this.female= (RadioButton) Super.findviewbyid ( R.id.female); This.sex.setOnCheckedChangeListener (new Oncheckedchangelisterimpl ());} Private Class OncheckedchangelisterImpl implements Oncheckedchangelistener{public void OnCheckedChanged (radiogroup group, int checkedid) {String temp=null ; if (Hello.this.male.getId () ==checkedid) {Temp=hello.this.male.gettext (). toString ();} if (Hello.this.female.getId () ==checkedid) {Temp=hello.this.female.gettext (). toString ();} Hello.this.text.setText ("Your gender is:" +temp);}}}

Operating effect:


SOURCE Download: http://download.csdn.net/detail/yayun0516/8363587

Android--oncheckedchangelistener instances

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.