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 = "female" 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.