Radio button for getting started with Android (RadioButton)

Source: Internet
Author: User

MainActivity. java

Package com. jk. test; import android. app. activity; import android. OS. bundle; import android. view. menu; import android. widget. compoundButton; import android. widget. compoundButton. onCheckedChangeListener; import android. widget. radioButton; import android. widget. radioGroup; public class MainActivity extends Activity {private RadioButton eatButton; private RadioButton sleepButton; private RadioButton dotaButton; private RadioGroup groupButton; protected void onCreate (Bundle extension) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); eatButton = (RadioButton) findViewById (R. id. eatButtonid); sleepButton = (RadioButton) findViewById (R. id. sleepButtonid); dotaButton = (RadioButton) findViewById (R. id. dotaButtonid); RadioGroupListener listener1 = new RadioGroupListener (); groupButton. setOnCheckedChangeListener (listener1); RadioButtonListener listener2 = new RadioButtonListener (); eatButton. setOnCheckedChangeListener (listener2); sleepButton. setOnCheckedChangeListener (listener2); dotaButton. setOnCheckedChangeListener (listener2);} class RadioButtonListener implements OnCheckedChangeListener {public void onCheckedChanged (CompoundButton v, boolean isChecked) {System. out. println (v. getId () + "isChecked --->" + isChecked);} class RadioGroupListener implements android. widget. radioGroup. onCheckedChangeListener {public void onCheckedChanged (RadioGroup v, int checkedId) {if (checkedId = eatButton. getId () System. out. println ("eatButton selected"); else if (checkedId = sleepButton. getId () System. out. println ("selected sleepButton"); else if (checkedId = dotaButton. getId () System. out. println ("dotaButton selected") ;}} 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 ;}}

Activity_main.xml

<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" android: paddingBottom = "@ dimen/activity_vertical_margin" android: paddingLeft = "@ dimen/plugin" android: paddingRight = "@ dimen/plugin" android: paddingTop = "@ dimen/plugin" tools: context = ". mainActivity "> <RadioGroup android: id =" @ + id/radioGroupId "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: orientation = "horizontal"> <RadioButton android: id = "@ + id/eatButtonid" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "dinner"/> <RadioButton android: id = "@ + id/sleepButtonid" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "sleeping"/> <RadioButton android: id = "@ + id/dotaButtonid" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "dota"/> </RadioGroup> </LinearLayout>

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.