Android 基础知识4-3.5 RadioButton(单选按钮)&Checkbox(复选框)详解

本文介绍了Android中的RadioButton和Checkbox组件,包括它们的基本用法、事件处理、选中值的获取方式,以及如何自定义点击效果和调整文字与选择框的位置。RadioButton通常与RadioGroup配合实现单选功能,而Checkbox则用于多选。
摘要由CSDN通过智能技术生成

一、RadioButton(单选按钮)

1.1、简介

        RadioButton表示单选按钮,是button的子类,每一个按钮都有选择和未选中两种状态,经常与RadioGroup一起使用,否则不能实现其单选功能。RadioGroup继承自LinearLayout,可以使用Orientation属性控制RadioButton的排列方向。单项选择相信大家都不陌生吧。Android平台也提供了单项选择的组件,可以通过RadioGroup、RadioButton组合起来完成一个单项选择的效果。

1.2、基本用法与事件处理

        如题单选按钮,就是只能够选中一个,所以我们需要把RadioButton放到RadioGroup按钮组中,从而实现 单选功能!先熟悉下如何使用RadioButton,一个简单的性别选择的例子: 另外我们可以为外层RadioGroup设置orientation属性然后设置RadioButton的排列方式,是竖直(android:orientation="vertical")还是水平((android:orientation="horizontal")!

 1.2.1、获得选中的值有四种方式:

(1)、为RadioButton在xml中设置一个事件监听器

XML:

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

    <RadioButton
        android:id="@+id/radiobutton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="btnRadioButton1"
        android:text="男"
        android:textSize="30dp"/>

    <RadioButton
        android:id="@+id/radiobutton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="btnRadioButton2"
        android:text="女"
        android:textSize="30dp"/>
</RadioGroup>

Activity:

public class MainActivity extends AppCompatActivity {
    private RadioButton radioButton1;
    private RadioButton radioButton2;
    private TextView textView;

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

        radioButton1 = findViewById(R.id.radiobutton1);
        radioButton2 = findViewById(R.id.radiobutton2);
        textView = findViewById(R.id.textview);

    }

    public void btnRadioButton1(View view) {
        textView.setText("您的性别为男");
    }

    public void btnRadioButton2(View view) {
        textView.setText("您的性别为女");
    }
}

效果图:

 

(2)、为RadioButton设置一个事件监听器setOnCheckChangeListener

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="请选择性别?"
        android:textSize="20sp"
        android:textStyle="bold"
        android:layout_margin="10dp"/>

    <RadioGroup
        android:id="@+id/radiogroup"
        android:layout_width="match_parent"
        android:layout_height=&
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yyxhzdm

你的鼓励是我创作的最大动力!!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值