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