Basic android control learning ----- RadioButton & CheckBox, basic android Control
Explanation of RadioButton and CheckBox:
I. Basic usage and event handling
(1) RadioButton is a single listener, which means only one of them can be selected. We need to put RadioButton in RadioGroup for use, in addition, we can set the orientation attribute in RadioGroup to control the direction of a single queue.
<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical" android: background = "# ffffff"> <TextView android: id = "@ + id/text1" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: text = "Please select your gender" android: textStyle = "bold" android: textSize = "30sp"/> <RadioGroup android: id = "@ + id/rg" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: orientation = "horizontal"> <RadioButton android: id = "@ + id/rb1." android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "male"/> <RadioButton android: id = "@ + id/rb2" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = ""/> </RadioGroup> <Button android: id = "@ + id/btn1" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: text = ""/> </LinearLayout>
(2) How can we obtain the selected values of the single-choice button? There are two methods:
A: Set setonCheckChangeListener for RadioGroup (radioButton ).
Package com. example. test3; import android. app. activity; import android. OS. bundle; import android. widget. radioButton; import android. widget. radioGroup; import android. widget. toast; public class MainActivity extends Activity {private RadioGroup radioGroup; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); radioGroup = (RadioGroup) findViewById (R. id.; radioGroup. setOnCheckedChangeListener (new RadioGroup. onCheckedChangeListener () {@ Override public void onCheckedChanged (RadioGroup radioGroup, int checkId) {RadioButton radioButton = (RadioButton) findViewById (checkId); Toast. makeText (MainActivity. this, "you selected" + radioButton. getText (). toString (), Toast. LENGTH_LONG ). show ();}});}}
B: Set setOnClickListener for RadioGroup. However, when using this method, you need
Package com. example. test3; import android. app. activity; import android. OS. bundle; import android. view. view; import android. widget. button; import android. widget. radioButton; import android. widget. radioGroup; import android. widget. toast; public class MainActivity extends Activity {private RadioGroup radioGroup; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); radioGroup = (RadioGroup) findViewById (R. id. rg1); Button btn = (Button) findViewById (R. id. btn1); btn. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View view) {for (int I = 0; I <radioGroup. getChildCount (); I ++) {RadioButton radioButton = (RadioButton) radioGroup. getChildAt (I); if (radioButton. isChecked () {Toast. makeText (MainActivity. this, "you selected" + radioButton. getText (), Toast. LENGTH_LONG ). show (); break ;}}}});}}
(3) CheckedButto is similar to RadioButton. Check the Code directly.
<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical" android: background = "# ffffff"> <TextView android: id = "@ + id/text1" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: text = "Please select your favorite fruit (you can select multiple)" android: textStyle = "bold" android: textSize = "22sp"/> <CheckBox android: id = "@ + id/cb1" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "apple"/> <CheckBox android: id = "@ + id/cb2" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "banana"/> <CheckBox android: id = "@ + id/cb3" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Li Zi"/> <Button android: id = "@ + id/btn1" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: text = "Submit"/> </LinearLayout>
Package com. example. test3; import android. app. activity; import android. OS. bundle; import android. view. view; import android. widget. button; import android. widget. checkBox; import android. widget. compoundButton; import android. widget. toast; public class MainActivity extends Activity implements CompoundButton. onCheckedChangeListener, View. onClickListener {private CheckBox checkBox1; private CheckBox checkBox2; private CheckBox checkBox3; private Button btn1; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); checkBox1 = (CheckBox) findViewById (R. id. cb1); checkBox2 = (CheckBox) findViewById (R. id. cb2); checkBox3 = (CheckBox) findViewById (R. id. cb3); btn1 = (Button) findViewById (R. id. btn1); listener (this); checkBox2.setOnCheckedChangeListener (this); listener (this); btn1.setOnClickListener (this) ;}@ Override public void onCheckedChanged (CompoundButton compoundButton, boolean B) {if (compoundButton. isChecked () {Toast. makeText (MainActivity. this, "you selected" + compoundButton. getText (), Toast. LENGTH_LONG ). show () ;}@ Override public void onClick (View view) {String choose = ""; if (checkBox1.isChecked () {choose + = checkBox1.getText (). toString ();} if (checkBox2.isChecked () {choose + = checkBox2.getText (). toString ();} if (checkBox3.isChecked () {choose + = checkBox3.getText (). toString ();} Toast. makeText (MainActivity. this, "you selected" + choose, Toast. LENGTH_LONG ). show ();}}
Ii. Effect of custom clicks or custom click boxes (taking checkBox as an example)
There are two methods, but the two methods are essentially the same.
(1) Method 1: The method is as simple as the Button mentioned above.
Define the StateListDrawable File
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="true" android:state_enabled="true" android:drawable="@mipmap/btn_radio_on"/> <item android:state_checked="false" android:state_enabled="true" android:drawable="@mipmap/btn_radio_off"/></selector>
Use the button attribute in the layout file.
(2) custom style
Step 1: Define the StateListDrawable file first.
Step 2: Define custom styles in the style File
Step 3: Use style in the layout File
: