Chapter 4 (4) single choice and check, Chapter 2 single choice check

Source: Internet
Author: User

Chapter 4 (4) single choice and check, Chapter 2 single choice check

Category: C #, Android, VS2015;

Created on: 1. Introduction

1. CheckBox

Check

[Checked] property: whether to select.

2. RadioButton

Single choice

[Checked] property: whether to select.

[RadioGroup] attribute: RadioButton group container. Note that RadioButton must be included in the RadioGroup. Ii. Example 4-Demo04CheckBoxRadioButton

1. Run

2. Add the demo04_CheckBoxRadioButton.axml file.

Add the file in the layout folder.

Drag and Drop two [CheckBox] controls and one [RadioGroup] control from the [toolbox] to the design interface, and then directly modify them to the following content in [Source:

<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android =" http://schemas.android.com/apk/res/android "Android: orientation =" vertical "android: layout_width =" match_parent "android: layout_height =" match_parent "> <CheckBox android: text =" red "android: layout_width = "match_parent" android: layout_height = "wrap_content" android: id = "@ + id/checkBoxRed"/> <CheckBox android: text = "green" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: id = "@ + id/checkBoxGreen"/> <RadioGroup android: minWidth = "25px" android: minHeight = "25px" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: id = "@ + id/radioGroupGander"> <RadioButton android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: checked = "true" android: text = "male" android: id = "@ + id/radioButtonMale"/> <RadioButton android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "" android: id = "@ + id/radioButtonFamale"/> </RadioGroup> <Button android: id = "@ + id/btnOK" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: text = "OK"/> </LinearLayout>

3. Add the Demo04CheckBoxRadioButton. cs file.

Add the file in the SrcActivity folder.

Using System; using Android. app; using Android. OS; using Android. widget; namespace ch05demos. srcActivity {[Activity (Label = "CheckBoxRadioButtonDemo")] public class failed: Activity {CheckBox red, green; RadioButton nan, nv; protected override void OnCreate (Bundle savedInstanceState) {base. onCreate (savedInstanceState); SetContentView (Resource. layout. demo04_CheckBoxRadioButton); re D = FindViewById <CheckBox> (Resource. id. checkBoxRed); green = FindViewById <CheckBox> (Resource. id. checkBoxGreen); nan = FindViewById <RadioButton> (Resource. id. radioButtonMale); nv = FindViewById <RadioButton> (Resource. id. radioButtonFamale); var button = FindViewById <Button> (Resource. id. btnOK); button. click + = Button_Click;} private void Button_Click (object sender, EventArgs e) {string s1 = "Gender:" + (Nan. Checked? "Male": "female"); string s2 = "Favorite color:"; if (red. checked) s2 + = red. text; if (green. checked) s2 + = "" + green. text; Toast. makeText (this, string. format ("{0} \ n {1}", s1, s2), ToastLength. long ). show ();}}}

Run and observe the effect of this example.

Tip: You can use the [Checked] attribute or the Toggle () method to change the RadioButton status.

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.