RadioButton exercise (android)

Source: Internet
Author: User

Radio is composed of RadioGroup and RadioButton. This is very simple.

Main. xml

<? 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"
Android: orientation = "vertical">

<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "your gender: select the answer"/>
<RadioGroup
Android: id = "@ + id/rg1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: orientation = "vertical"
Android: layout_x = "3px"
Android: layout_y = "54px"
>
<RadioButton
Android: id = "@ + id/rb1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "boys"
/>
<RadioButton
Android: id = "@ + id/rb2"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = ""
/>
</RadioGroup>
<Button
Android: id = "@ + id/btn"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "test"
/>
</LinearLayout>

 

Code

 

Package zziss. android. radiotest;

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 RadioTestActivity extends Activity {
/** Called when the activity is first created .*/
RadioGroup rg1;
RadioButton rb1;
RadioButton rb2;
Button ibtn;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Rg1 = (RadioGroup) this. findViewById (R. id. rg1 );
Rb1. = (RadioButton) this. findViewById (R. id. rb1 );
Rb2 = (RadioButton) this. findViewById (R. id. rb2 );

Rg1.setOnCheckedChangeListener (new RadioGroup. OnCheckedChangeListener (){

@ Override
Public void onCheckedChanged (RadioGroup group, int checkedId ){
// TODO Auto-generated method stub
If (checkedId = rb1.getId ())
{
ShowInfo (rb1.getText (). toString ());
}
If (checkedId = rb2.getId ())
{
ShowInfo (rb2.getText (). toString ());
}
}
});

Ibtn = (Button) this. findViewById (R. id. btn );
Ibtn. setOnClickListener (new View. OnClickListener (){

@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
Int id = rg1.getCheckedRadioButtonId ();
If (id! =-1)
ShowIdText (id );
Else
ShowInfo ("select ");
}
});
}

Private void ShowInfo (String str)
{
Toast toast = Toast. makeText (this, str, Toast. LENGTH_SHORT );
Toast. show ();
}
Private void showIdText (int aId)
{
ShowInfo (RadioButton) this. findViewById (aId). getText (). toString ());
}

}

 

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.