Android basic getting started -- 2.3.5.RadioButton (single choice button) & amp; Checkbox (check box), android single choice button

Source: Internet
Author: User

Android basic getting started -- 2.3.5.RadioButton (single choice button) & Checkbox (check box), android single choice button
Basic Android tutorial -- 2.3.5.RadioButton (single choice button) & Checkbox (check box)

Tags (separated by spaces): basic Android tutorial

This section introduces:

This section introduces RadioButton and Checkbox in the basic Andoird UI control;
The content to be explained in this section is: RadioButton and Checkbox
** 1. Basic usage
2. event handling;
3. Custom click Effect;
4. Change the relative position of the text and selection box;
5. Modify the distance between the text and the selection box **

In fact, many of these two controls are similar, except single-choice and multi-choice, event processing, and others are similar!
In addition, there is a problem of incorrect Checkbox in ListView. We will solve this problem in the ListView chapter.
Okay. Start this section ~
Official documentation API: RadioButton; CheckBox;

1. Basic usage and event handling: 1) RadioButton (single choice button)

For example, you can select only one single-question button. Therefore, you need to put RadioButton in the RadioGroup button group to implement
Single choice function! First, familiarize yourself with how to use RadioButton. A simple example of gender selection:
In addition, we can set the orientation attribute for the outer RadioGroup and then set the arrangement of RadioButton, vertical or horizontal ~

:

PS: my mobile phone is Android 5.0.1. Here, RadioButton is slightly better than RadioButton of the old version ~

The layout code is as follows:

<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: id = "@ + id/LinearLayout1" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical" tools: context = ". mainActivity "> <TextView android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: text =" select gender "android: textSize = "23dp"/> <RadioGroup android: id = "@ + id/radioGroup" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: orientation = "horizontal"> <RadioButton android: id = "@ + id/btnMan" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "male" android: checked = "true"/> <RadioButton android: id = "@ + id/btnWoman" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = ""/> </RadioGroup> <Button android: id = "@ + id/btnpost" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = ""/> </LinearLayout>

Obtain the selected value:

There are two methods,

FirstIs to set an event listener setOnCheckChangeListener for RadioButton

The sample code is as follows:

RadioGroup radgroup = (RadioGroup) findViewById (R. id. radioGroup); // method for obtaining the value of a single-choice button // set a listener for radioGroup: setOnCheckedChanged () radgroup. listener (new OnCheckedChangeListener () {@ Override public void onCheckedChanged (RadioGroup group, int checkedId) {RadioButton radbtn = (RadioButton) findViewById (checkedId); Toast. makeText (getApplicationContext (), "the button group value has changed. You have selected" + radbtn. getText (), Toast. LENGTH_LONG ). show ();}});

Run:

PS: add an id for each RadioButton. Otherwise, the radio function will take effect !!!

SecondYou can click another button to obtain the value of the selected radio button. You can also directly obtain the value ~

The sample code is as follows:

Button btnchange = (Button) findViewById (R. id. btnpost); RadioGroup radgroup = (RadioGroup) findViewById (R. id. radioGroup); // set a listener for radioGroup: setOnCheckedChanged () btnchange. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {for (int I = 0; I <radgroup. getChildCount (); I ++) {RadioButton rd = (RadioButton) radgroup. getChildAt (I); if (rd. isChecked () {Toast. makeText (getApplicationContext (), "click the submit button to obtain the information you choose:" + rd. getText (), Toast. LENGTH_LONG ). show (); break ;}}}});

Run:

Code parsing:
Here we have set a setOnClickListener event listener for the submit button. If you click it every time, traverse the RadioGroup to determine which button is selected. We can obtain related information about RadioButton through the following methods!
GetChildCont() Obtain the number of radio buttons in the button group;
GetChinldAt(I): obtain our single-choice button Based on the index value
IsChecked(): Determines whether the button is selected.

2) CheckBox (check box)

You can select multiple options at the same time as the check box. There are two ways to obtain the selected values:
1. Add an event for each CheckBox: setOnCheckedChangeListener
2. Get a button and click it to judge each checkbox: isChecked ();

Run:

Implementation Code:

public class MainActivity extends AppCompatActivity implements View.OnClickListener,CompoundButton.OnCheckedChangeListener{    private CheckBox cb_one;    private CheckBox cb_two;    private CheckBox cb_three;    private Button btn_send;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        cb_one = (CheckBox) findViewById(R.id.cb_one);        cb_two = (CheckBox) findViewById(R.id.cb_two);        cb_three = (CheckBox) findViewById(R.id.cb_three);        btn_send = (Button) findViewById(R.id.btn_send);        cb_one.setOnCheckedChangeListener(this);        cb_two.setOnCheckedChangeListener(this);        cb_three.setOnCheckedChangeListener(this);        btn_send.setOnClickListener(this);    }    @Override    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {       if(compoundButton.isChecked()) Toast.makeText(this,compoundButton.getText().toString(),Toast.LENGTH_SHORT).show();    }    @Override    public void onClick(View view) {        String choose = "";        if(cb_one.isChecked())choose += cb_one.getText().toString() + "";        if(cb_two.isChecked())choose += cb_two.getText().toString() + "";        if(cb_three.isChecked())choose += cb_three.getText().toString() + "";        Toast.makeText(this,choose,Toast.LENGTH_SHORT).show();    }}
2. Custom click Effect

Although the RadioButton and Checkbox versions after 5.0 are a little better than the old version
You may not like it or want it. You need to click the effect on your own! It is easy to implement. First write a custom
Selctor resource, set the switching image when selected or not selected ~!

The implementation is as follows:

PS: the reason for this material is a little...

Implementation Code:

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item        android:state_enabled="true"        android:state_checked="true"        android:drawable="@mipmap/ic_checkbox_checked"/>    <item        android:state_enabled="true"        android:state_checked="false"        android:drawable="@mipmap/ic_checkbox_normal" /></selector>

After writing it, we can set it in two ways! You can see it ~

① Android: the button property is set to the selctor above.

android:button="@drawable/rad_btn_selctor"

② Define an attribute in the style, and then use the android style attribute settings to add the following code to the style:

    <style name="MyCheckBox" parent="@android:style/Widget.CompoundButton.CheckBox">        <item name="android:button">@drawable/rad_btn_selctor</item>    </style>

Then layout it there:

style="@style/MyCheckBox"
3. Change the relative position of the text and selection box

This implementation is also very simple. Do you still remember the drawableXxx we used when learning TextView?
To control the position of the Selection box, just two steps! Settings:

Step 1.Android: button = "@ null"
Step 2.Android: drawableTop = "@ android: drawable/btn_radio"
Of course, we can replace drawableXxx with what we like!

4. Modify the distance between the text and the selection box

Sometimes, we may need to adjust the distance between the text and the selection box to make them look a little less crowded. We can:
1. Control in XML code:
Use android: paddingXxx = "xxx" to control the distance
2. In Java code, it is better to dynamically calculate paddingLeft!

The sample code is as follows:

 rb.setButtonDrawable(R.drawable.rad_btn_selctor); int rb_paddingLeft = getResources().getDrawable(R.mipmap.ic_checkbox_checked).getIntrinsicWidth()+5; rb.setPadding(rb_paddingLeft, 0, 0, 0);
Summary:

Okay. Here we will talk about RadioButton and Checkbox. If you have any wrong or bad information, or if you have any good suggestions, please kindly advise.
Very grateful ~ Thank you...

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.