• 【Android从零单排系列十】《Android视图控件——RadioButton》


    目录

    前言

    一.RadioButton基本介绍

    二.RadioButton常用主要属性介绍

    三.RadioGroup中RadioButton使用的常见问题

    四.基础DEMO示例


    前言

    小伙伴们,在上文中我们介绍了Android视图控件ImageView控件,本文我们继续盘点,介绍一下视图控件的第五个控件——RadioButton。

    一.RadioButton基本介绍

     在 Android 应用开发中,RadioButton是单选按钮,允许用户在一个组中选择一个选项。同一组中的单选按钮有互斥效果。

    二.RadioButton常用主要属性介绍

    (1)button属性:主要用于图标大小要求不高,间隔要求也不高的场合。

    (2)background属性:主要用于能够以较大空间显示图标的场合。

    (3)drawableLeft属性:主要用于对图标与文字之间的间隔有要求的场合。

    注意使用 background 或者 drawableLeft时 要设置 android:button="@null"

    三.RadioGroup中RadioButton使用的常见问题

    1.radiogroup中的radiobutton如何设置默认选中,可以看很早之前写的这篇文章。

    RadioGroup中RadioButton默认选中问题

    2.相信用过RadioGroup的同学都踩过很多坑,其中之一就是这个控件设计的不是很合理,不能设置里面的radiobutton的 排列方式(几行几列),导致我们开发的时候要调整里面的布局很是麻烦。

    Radiogroup内如果有多个RadioButton如何设置自动换行并且保留点击事件,这个可以看我很早之前写的一篇文章 RadioGroup 自动换行且保留点击事件

    3.适用于较少类型的  radiobutton单选换行功能

     Android 实现radiobutton单选换行效果

    四.基础DEMO示例

    activity_radiobutton.xml

    1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    2. xmlns:app="http://schemas.android.com/apk/res-auto"
    3. xmlns:tools="http://schemas.android.com/tools"
    4. android:layout_width="match_parent"
    5. android:layout_height="match_parent"
    6. tools:context=".MainActivity"
    7. android:orientation="vertical">
    8. <TextView
    9. android:id="@+id/button"
    10. android:text="【Android从零单排系列十】《Android视图控件——RadioButton》"
    11. android:background="@drawable/btn_selector"
    12. android:layout_width="match_parent"
    13. android:layout_marginTop="30dp"
    14. android:layout_marginLeft="20dp"
    15. android:layout_marginRight="20dp"
    16. android:layout_height="wrap_content"/>
    17. <RadioGroup
    18. android:id="@+id/radioGroup"
    19. android:layout_width="match_parent"
    20. android:layout_height="wrap_content"
    21. android:layout_marginTop="20dp"
    22. android:gravity="center"
    23. android:orientation="horizontal">
    24. <RadioButton
    25. android:id="@+id/radioButton1"
    26. android:layout_width="wrap_content"
    27. android:layout_height="wrap_content"
    28. android:button="@null"
    29. android:checked="true"
    30. android:drawablePadding="10dp"
    31. android:layout_marginRight="30dp"
    32. android:drawableLeft="@drawable/radio_btn_selector"
    33. android:gravity="center"
    34. android:text="红色"
    35. android:textColor="#FF0033"/>
    36. <RadioButton
    37. android:id="@+id/radioButton2"
    38. android:layout_width="wrap_content"
    39. android:layout_height="wrap_content"
    40. android:button="@null"
    41. android:drawablePadding="10dp"
    42. android:drawableLeft="@drawable/radio_btn_selector"
    43. android:gravity="center"
    44. android:text="蓝色"
    45. android:textColor="#000000"/>
    46. RadioGroup>
    47. LinearLayout>
    RadioButtonActivity 
    1. package com.example.myapplication;
    2. import android.app.Activity;
    3. import android.graphics.Color;
    4. import android.os.Bundle;
    5. import android.widget.RadioButton;
    6. import android.widget.RadioGroup;
    7. public class RadioButtonActivity extends Activity {
    8. private RadioGroup radioGroup;
    9. private RadioButton radioButton1;
    10. private RadioButton radioButton2;
    11. @Override
    12. protected void onCreate(Bundle savedInstanceState) {
    13. super.onCreate(savedInstanceState);
    14. setContentView(R.layout.activity_radio_button);
    15. radioGroup = findViewById(R.id.radioGroup);
    16. radioButton1 = findViewById(R.id.radioButton1);
    17. radioButton2 = findViewById(R.id.radioButton2);
    18. radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    19. @Override
    20. public void onCheckedChanged(RadioGroup group, int checkedId) {
    21. if(radioButton1.isChecked()) {
    22. radioButton1.setTextColor(Color.RED);
    23. }else{
    24. radioButton1.setTextColor(Color.parseColor("#000000"));
    25. }
    26. if(radioButton2.isChecked()) {
    27. radioButton2.setTextColor(Color.RED);
    28. }else{
    29. radioButton2.setTextColor(Color.parseColor("#000000"));
    30. }
    31. }
    32. });
    33. }
    34. }

    3.radio_btn_selector

    1. <selector xmlns:android="http://schemas.android.com/apk/res/android">
    2. <item android:state_checked="true" android:drawable="@mipmap/on"/>
    3. <item android:drawable="@mipmap/off"/>
    4. selector>

  • 相关阅读:
    Docker 构建centos镜像yum报错,语言包下载报错
    Android5.1 文件AES加密
    数字图像处理复习
    【运维日常】mac刻录ubuntu系统进U盘,插入服务器安装系统
    React Fiber 入门浅学
    百战RHCE(第五十一战:运维工程师必会技-Ansible学习6-编写和执行Playbook)
    python05_流程控制
    Java - 对象克隆
    晚上弱光拍照不够清晰,学会这几招画面清晰效果好
    【软考 系统架构设计师】软件架构设计⑤ 软件架构评估
  • 原文地址:https://blog.csdn.net/shaoyezhangliwei/article/details/125502411