Basic ndroid Control

Source: Internet
Author: User

These two controls provide a good experience for users to make choices: for example, if users do not need to input them in person, we will provide users with more convenient options. The single-choice button (RadioButton) means that you can select only one option in this option. As the name suggests, the check box control can select multiple options. The following describes the two controls. 5.2.1 example: Example 1: Use of the RadioButton control (the layout file method is used for demonstration here. The usage of RadioButton is described first): <? 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"> <! -- A set of RadioButton is defined and divided into three options --> <RadioButton android: id = "@ + id/first_radiobutton" android: checked = "true" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: onClick = "onAction" android: text = "male"/> <RadioButton android: id = "@ + id/second_radiobutton" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: onClick = "onAction" android: text = "female"/> <RadioB Utton android: id = "@ + id/third_radiobutton" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: onClick = "onAction" android: text = ""/> </LinearLayout> the above shows the layout file code. Here, we mainly implement the layout interface in Figure 5.1, the code that really wants to implement the function is not here. In the above Code, android: checked = "true"; is used to set the selected option by default, while android: onClick = "onAction" is used to set the listener event method, in java code. Here, you can use RadioGroup to define a set of buttons, that is, in this group, the options are useful. Java code is used here. The Code is as follows: package xbb. bzq. android. app; import android. app. activity; import android. OS. bundle; import android. view. view; import android. widget. radioButton; public class extends Activity {// defines three RadioButton variables: private RadioButton mButton1, mButton2, mButton3; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); // instantiate three single-choice button controls mButton1 = (RadioButton) findViewById (R. id. first_radiobutton); mButton2 = (RadioButton) findViewById (R. id. second_radiobutton); mButton3 = (RadioButton) findViewById (R. id. third_radiobutton);}/*** this is the listener Method for implementation. It mainly implements the value of the modify option * @ param v */public void onAction (View v) {// determine the user's selection by obtaining the id, and then change the control's selection status switch (v. getId () {case R. id. first_radiobutton: mButton2.setChecked (false); mButton3.setChecked (false); mButton1.setChecked (true); break; case R. id. second_radiobutton: mButton1.setChecked (false); mButton3.setChecked (false); mButton2.setChecked (true); break; case R. id. third_radiobutton: mButton2.setChecked (false); mButton1.setChecked (false); mButton3.setChecked (true); break ;}}}

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.