The use of radio button (Radiogroup) in Android program development _android

Source: Internet
Author: User

has not introduced to everybody to introduce the radio button (Radiogroup) the use, first shows the effect chart for everybody:


XML file

<linearlayout
xmlns: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 " 
tools:context= ". Mainactivity " 
android:orientation=" vertical > 
<textview 
android:id= "@+id/txt" 
android: Layout_width= "Fill_parent" 
android:layout_height= "wrap_content" 
android:text= "your sex for"/> 
< Radiogroup 
android:id= "@+id/sex" 
android:layout_width= fill_parent "android:layout_height=" Wrap 
_content "> 
<radiobutton 
android:id=" @+id/male "android:text= 
" male " 
/> <radiobutton 
android:id= "@+id/female" 
android:text= "female"/> 
</RadioGroup> 

Java files

 public class Mainactivity extends activity {private TextView txt=null; 
Private Radiogroup Sex=null; 
Private RadioButton Male=null; 
Private RadioButton Female=null; 
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
Setcontentview (R.layout.activity_main); 
this.txt= (TextView) Super.findviewbyid (r.id.txt); 
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 Oncheckedchangelistenerimp ()); Private class Oncheckedchangelistenerimp implements oncheckedchangelistener{public void oncheckedchanged (Radiogroup 
Group, int checkedid) {String temp=null; 
if (MainActivity.this.male.getId () ==checkedid) {temp= "male"; 
else if (MainActivity.this.female.getId () ==checkedid) {temp= "female"; 
} MainActivity.this.txt.setText ("Your gender is" +temp); } }

The above is a small set to introduce the Android program development in the radio button (Radiogroup) The use of detailed, I hope to help you!

Related Article

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.