Android development-the use of the RadioButton radio button, androidradiobutton
Author: Qing Dujun
Address: http://blog.csdn.net/qingdujun/article/details/40679959
This article demonstrates how to use the Android single-choice button.
1) MainActivity. java
Package com. dg. radio; import android. app. activity; import android. OS. bundle; import android. view. menu; import android. view. menuItem; import android. widget. radioGroup; import android. widget. radioGroup. onCheckedChangeListener; import android. widget. toast; public class MainActivity extends Activity {RadioGroup rGroup; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // obtain the RadioGroup component rGroup = (RadioGroup) findViewById (R. id. radioGroup1); rGroup. setOnCheckedChangeListener (new OnCheckedChangeListener () {@ Overridepublic void onCheckedChanged (RadioGroup arg0, int arg1) {// TODO Auto-generated method stubswitch (arg1) {case R. id. male: Toast. makeText (getApplicationContext (), "Your selected gender is male", 0 ). show (); break; case R. id. female: Toast. makeText (getApplicationContext (), "Your selected gender is female", 1 ). show (); break; default: break; }}) ;}@ Override public boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}@ Override public boolean onOptionsItemSelected (MenuItem item) {// Handle action bar item clicks here. the action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest. xml. int id = item. getItemId (); if (id = R. id. action_settings) {return true;} return super. onOptionsItemSelected (item );}}
2) activity_main.xml
<RelativeLayout 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" android: paddingBottom = "@ dimen/activity_vertical_margin" android: paddingLeft = "@ dimen/plugin" android: paddingRight = "@ dimen/plugin" android: paddingTop = "@ dimen/activity_v Ertical_margin "tools: context =" com. dg. radio. mainActivity "> <RadioGroup android: id =" @ + id/radioGroup1 "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: layout_alignParentTop =" true "android: layout_centerHorizontal = "true" android: layout_marginTop = "134dp"> <! -- The default option is male --> <RadioButton android: id = "@ + id/male" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: checked = "true" android: text = "male"/> <RadioButton android: id = "@ + id/female" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = ""/> </RadioGroup> </RelativeLayout>
3) Running Effect
Address: http://blog.csdn.net/qingdujun/article/details/40679959
In Android development, how does one obtain the check value of this radiobutton through the radiobutton id?
RadioButton B = (RadioButton) this-> findviewid (your id );
Then you can check whether its check attribute is B. isChecked () to determine whether it is true or false. Try it. I doubt the prospect of android because it has just arrived.
Android RadioButton attributes
Android: state_checked = "false"
Android: state_checked = "true"
This is what it means .. You try,