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>


效果图如下:
[img]http://dl2.iteye.com/upload/attachment/0127/2670/17f9d06e-6ebb-3058-af54-fffef9b7a9a4.png[/img]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值