Android Studio special button RadioButton

1. RadioButton radio button 
    RadioButton (radio button) is widely used in Android development. For example, when some options are selected, radio buttons are used. It is a two-state button with a single circular radio button that can be selected or unselected. When the RadioButton is not selected, the user can press or click to select it. However, in contrast to checkboxes, once checked, the user cannot uncheck them.
    The implementation of RadioButton consists of two parts, that is, RadioButton and RadioGroup are used together. RadioGroup is a single-selection combo box, which can hold multiple RadioButton containers. In the absence of RadioGroup, RadioButton can all be selected; when multiple RadioButtons are contained by RadioGroup In the case of RadioButton, only one can be selected. And use setOnCheckedChangeListener to monitor the radio button
The following specific example:

java code -

package com.example.administrator.myapplication3;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.util.DisplayMetrics;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import static java.security.AccessController.getContext;

/**
 * Created by Administrator on 2017/9/27 0027.
 */

public class Language extends Activity {

    private RadioGroup language;
    private RadioButton ch_1;
    private RadioButton ch_2;
    private RadioButton en_1;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView(R.layout.language_main);

        //get instance
        language = (RadioGroup) findViewById(R.id.language);
        ch_1 = (RadioButton)findViewById(R.id.ch1);
        ch_2 = (RadioButton)findViewById(R.id.ch2);
        en_1 = (RadioButton)findViewById(R.id.en1);

        //set monitor
        language.setOnCheckedChangeListener(new RadioGroupListener());

    }

    //Define a RadioGroup's OnCheckedChangeListener
    //RadioGroup is bound to RadioGroup.OnCheckedChangeListener
    //CheckBox is bound to CompoundButton.OnCheckedChangeListener or view.OnClickListener
     class RadioGroupListener implements RadioGroup.OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, @IdRes int checkedId) {
            if (checkedId==ch_1.getId()){
                Toast.makeText(Language.this, "Simplified Chinese is selected", Toast.LENGTH_SHORT).show();
            }else if (checkedId==ch_2.getId()){
                Toast.makeText(Language.this, "Traditional Chinese is selected", Toast.LENGTH_SHORT).show();
            }else if (checkedId==en_1.getId()){
                Toast.makeText(Language.this, "选中了English", Toast.LENGTH_SHORT).show();
            }
        }
    }
}


xml code -

<?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">

    <RadioGroup
        android:id="@+id/language"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <RadioButton
            android:id="@+id/ch1"
            android:text="Simplified Chinese"
            android:textSize="25dp"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:button="@null"
            android:drawableRight="@android:drawable/btn_radio"
            android:checked="true" />

        <RadioButton
            android:id="@+id/ch2"
            android:text="Traditional Chinese"
            android:textSize="25dp"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:button="@null"
            android:drawableRight="@android:drawable/btn_radio"
            android:checked="true" />

        <RadioButton
            android:id="@+id/en1"
            android:text="English"
            android:textSize="25dp"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:button="@null"
            android:drawableRight="@android:drawable/btn_radio"
            android:checked="true" />
    </RadioGroup>
</LinearLayout>


The effect diagram is as follows:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326118887&siteId=291194637