Android单选按钮RadioButton的使用方法

单选按钮要在一组中选择一项,并且不能多选。

同一组RadioButton要放在同一个RadioGroup节点下。

RadioButton默认未选中,点击后选中但是再次点击不会取消选中。

RadioButton经常会更换按钮图标,如果通过button属性变更图标,那么图标与文字就会挨得很近。为了拉开图标与文字之间的距离,得换成drawableLeft属性展示新图标(不要忘记把button改为@null),再设置drawablePadding即可指定间隔距离。

复现代码时出现了一个错误,处理单选按钮的响应,要先写一个单选监听器实现接口 RadioGroup.OnCheckedChangeListener,而不是复合按钮的CompoundButton.OnCheckedChangeListener。

MainActivity

package com.example.middle;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class RadioVerticalActivity extends AppCompatActivity implements OnCheckedChangeListener {
    private TextView tv_marry; // 声明一个文本视图对象

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_radio_vertical);
        // 从布局文件中获取名叫tv_marry的文本视图
        tv_marry = findViewById(R.id.tv_marry);
        // 从布局文件中获取名叫rg_marry的单选组
        RadioGroup rg_marry = findViewById(R.id.rg_marry);
        // 给rg_marry设置单选监听器,一旦用户点击组内的单选按钮,就触发监听器的onCheckedChanged方法
        rg_marry.setOnCheckedChangeListener(this);
    }

    // 在用户点击组内的单选按钮时触发
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (checkedId == R.id.rb_married) {
            tv_marry.setText("哇哦,祝你早生贵子");
        } else if (checkedId == R.id.rb_unmarried) {
            tv_marry.setText("哇哦,你的前途不可限量");
        }
    }

}

Layout

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="请选择您的婚姻状况"
        android:textColor="#000000"
        android:textSize="17sp" />

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

        <!-- 通过button属性修改单选按钮的图标 -->
        <RadioButton
            android:id="@+id/rb_unmarried"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:button="@drawable/radio_selector"
            android:text="未婚"
            android:textColor="#000000"
            android:textSize="17sp" />

        <!-- 通过drawableLeft属性修改单选按钮的图标 -->
        <RadioButton
            android:id="@+id/rb_married"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:button="@null"
            android:drawableLeft="@drawable/radio_selector"
            android:drawablePadding="10dp"
            android:text="已婚"
            android:textColor="#000000"
            android:textSize="17sp" />
    </RadioGroup>

    <TextView
        android:id="@+id/tv_marry"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:textSize="17sp" />
</LinearLayout>

result

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Android单选按钮RadioButton的使用详解

    RadioButton是最普通的UI组件之一,继承了Button类,可以直接使用Button支持的各种属性和方法. RadioButton与普通按钮不同的是,它多了一个可以选中的功能,可额外指定一个android:checked属性,该属性可以指定初始状态时是否被选中,其实也可以不用指定,默认初始状态都不选中. 使用RadioButton必须和单选框RadioGroup一起使用,在RadioGroup中放置RadioButton,通过setOnCheckedChangeListener( )来响

  • Android ListView ImageView实现单选按钮实例

    做Android开发两年的时间,技术稍稍有一些提升,刚好把自己实现的功能写出来,记录一下,如果能帮助到同行的其他人,我也算是做了件好事,哈哈!!废话不多说,先上个图. 先上一段代码: 1 if (lastposition == position){ 2 viewHolder.setImageResource(R.id.iv_yuandian1,R.mipmap.ic_button_checked); 3 } else { 4 viewHolder.setImageResource(R.id.iv

  • Android程序开发中单选按钮(RadioGroup)的使用详解

    在还没给大家介绍单选按钮(RadioGroup)的使用,先给大家展示下效果图吧: xml文件 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_heig

  • Android编程实现带有单选按钮和复选按钮的dialog功能示例

    本文实例讲述了Android编程实现带有单选按钮和复选按钮的dialog.分享给大家供大家参考,具体如下: 带有单选按钮的dialog: package example.com.myapplication; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.DialogInterface; import android.os.

  • android实现单选按钮功能

    在我们平时在注册个人信息的时候,经常会让我们选择是男生还是女生,那么这个单选框在Android中是怎么实现的呢?现在我们就来学习一下吧 首先我们要明白实现这样一个效果需要哪几部? 1.在layout布局文件中建立一个文件,我起的名字为activity_radio.xml 代码为: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schem

  • Android单选按钮对话框用法实例分析

    本文实例讲述了Android单选按钮对话框用法.分享给大家供大家参考.具体如下: main.xml布局文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:l

  • Android单选按钮RadioButton的使用方法

    单选按钮要在一组中选择一项,并且不能多选. 同一组RadioButton要放在同一个RadioGroup节点下. RadioButton默认未选中,点击后选中但是再次点击不会取消选中. RadioButton经常会更换按钮图标,如果通过button属性变更图标,那么图标与文字就会挨得很近.为了拉开图标与文字之间的距离,得换成drawableLeft属性展示新图标(不要忘记把button改为@null),再设置drawablePadding即可指定间隔距离. 复现代码时出现了一个错误,处理单选按钮

  • Android复选框CheckBox与开关按钮Switch及单选按钮RadioButton使用示例详解

    目录 前言 一.复选框CheckBox 二.开关按钮Switch 三.单选按钮RadioButton 单选组的用法 前言 CompoundButton在XML文件中主要使用下面两个属性. checked:指定按钮的勾选状态,true表示勾选,false则表示未勾选,默认为未勾选. button:指定左侧勾选图标的图形资源,如果不指定就使用系统的默认图标. CompoundButton在java代码中主要使用下列4种方法. setChecked:设置按钮的勾选状态. setButtonDrawab

  • Android 多种dialog的实现方法(推荐)

    要求:设计如下界面,包括图片按钮.复选按钮.单选按钮.普通按钮,单击按钮弹出对话框,运行效果如下: string.xml文件源代码: <resources> <string name="app_name">多种弹出对话框</string> <string name="dialog_img">图片按钮</string> <string name="dialog_radio">单

  • Android webview实现拍照的方法

    Android webview实现拍照的方法 1. html <div id="pnlVideo1"> <input type="hidden" name="imgNric1" id="imgNric1" /> <label id="nric" class="control-label labelfont" style="color:#888;fo

  • android针对json数据解析方法实例分析

    本文实例讲述了android针对json数据解析方法.分享给大家供大家参考.具体如下: JSON的定义: 一种轻量级的数据交换格式,具有良好的可读和便于快速编写的特性.业内主流技术为其提供了完整的解决方案(有点类似于正则表达式 ,获得了当今大部分语言的支持),从而可以在不同平台间进行数据交换.JSON采用兼容性很高的文本格式,同时也具备类似于C语言体系的行为. – Json.org JSON Vs XML 1.JSON和XML的数据可读性基本相同 2.JSON和XML同样拥有丰富的解析手段 3.

  • Android实现定制桌面的方法

    本文实例讲述了Android实现定制桌面的方法.分享给大家供大家参考.具体如下: launcher也就是我们的Home,可以简单地把它理解为一个简化的linux GUI.作为一个GUI它首先必须完成它最本分的功能,就是它必须能提供对所有应用程序(CATEGORY_LAUNCHER)的映射:不过作为一个 GUI,它除了做好本分之外还必须是符合大众审美的美女(wallpaper):另外还必须具有良好的交互性,没有良好的交互性就像你对一位美女殷勤了半天,她却直接对无视,那结果是比较糟糕的-- 所谓兵马

  • Android控件ListView使用方法详解

    Android控件ListView使用方法介绍,具体如下 一.ListView的简单用法 首先新建一个ListViewTest项目,并让Android Studio自动创建好活动.然后修改activity_main.xml中的代码,如下: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/re

  • Android使用vcard文件的方法简单实例

    本文实例讲述了Android使用vcard文件的方法.分享给大家供大家参考,具体如下: FileOutputStream os = null; try { os = VCardTest.this.openFileOutput("Android.vcf", MODE_PRIVATE); } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } Output

  • 详解Android中Intent的使用方法

    一.Intent的用途 Intent主要有以下几种重要用途: 1. 启动Activity:可以将Intent对象传递给startActivity()方法或startActivityForResult()方法以启动一个Activity,该Intent对象包含了要启动的Activity的信息及其他必要的数据. 2. 启动Service:可以将Intent对象传递给startService()方法或bindService()方法以启动一个Service,该Intent对象包含了要启动的Service的

随机推荐