Android Studio 特殊按钮RadioButton

一.RadioButton单选按钮 
    RadioButton(单选按钮)在Android开发中应用的非常广泛,比如一些选择项的时候,会用到单选按钮。它是一种单个圆形单选框双状态的按钮,可以选择或不选择。在RadioButton没有被选中时,用户能够按下或点击来选中它。但是,与复选框相反,用户一旦选中就不能够取消选中。
    实现RadioButton由两部分组成,也就是RadioButton和RadioGroup配合使用.RadioGroup是单选组合框,可以容纳多个RadioButton的容器.在没有RadioGroup的情况下,RadioButton可以全部都选中;当多个RadioButton被RadioGroup包含的情况下,RadioButton只可以选择一个。并用setOnCheckedChangeListener来对单选按钮进行监听
下面的具体的例子:

java代码——

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);

        //获取实例
        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);

        //设置监听
        language.setOnCheckedChangeListener(new RadioGroupListener());

    }

    //定义一个RadioGroup的OnCheckedChangeListener
    //RadioGroup  绑定的是RadioGroup.OnCheckedChangeListener
    //CheckBox    绑定的是CompoundButton.OnCheckedChangeListener 或者 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, "选中了简体中文", Toast.LENGTH_SHORT).show();
            }else if (checkedId==ch_2.getId()){
                Toast.makeText(Language.this, "选中了繁体中文", Toast.LENGTH_SHORT).show();
            }else if (checkedId==en_1.getId()){
                Toast.makeText(Language.this, "选中了English", Toast.LENGTH_SHORT).show();
            }
        }
    }
}


xml代码——

<?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="简体中文"
            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="繁体中文"
            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>


效果图如下:

猜你喜欢

转载自wenweijie.iteye.com/blog/2395474