服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android基础控件RadioGroup使用方法详解

Android基础控件RadioGroup使用方法详解

2022-11-07 15:14你的益达 Android

这篇文章主要为大家详细介绍了Android基础控件RadioGroup的使用方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文为大家分享了Android基础控件RadioGroup的使用,供大家参考,具体内容如下

1.简单介绍

RadioGroup可以提供几个选项供用户选择,但只能选择其中的一个。其下面可以横着或者竖着挂几个RadioButton,也可以挂载其他控件(如TextView)。RadioGroup的相应事件一般不由下面的RadioButton响应,而是直接由RadioGroup响应。实现RadioGroup.OnCheckedChangeListener接口即可监听RadioGroup。RadioButton也是派生自CompoundButton,也可以通过修改button属性来修改图标,但是通过button属性修改往往会使文字和图标挨得很近。这时候我们可以设置RadioButton的drawableLeft属性和drawablePadding属性来使图标和文字挨得远一点(同时把button属性设置成@null)。下图是RadioGroup的使用效果。

Android基础控件RadioGroup使用方法详解

2.简单使用

下面是RadioGroup的简单实现代码。

radio_group_selector.xml

?
1
2
3
4
5
6
7
8
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <!--选中-->
  <item android:state_checked="true" android:drawable="@drawable/radio_choose"/>
 
  <!--普通状态-->
  <item android:drawable="@drawable/radio_unchoose"/>
</selector>

activity_radio_group.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".RadioGroupActivity"
  android:orientation="vertical">
 
  <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="18sp"
    android:textColor="#000000"
    android:text="这是横着放的RadioGroup"/>
 
  <RadioGroup
    android:id="@+id/rg_horizontal_demo"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
 
    <RadioButton
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:checked="false"
      android:text="好"
      android:textSize="18sp"
      android:id="@+id/rb_horizontal_good"
      android:textColor="#000000"/>
 
    <RadioButton
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:checked="false"
      android:text="很好"
      android:textSize="18sp"
      android:id="@+id/rb_horizontal_very_good"
      android:textColor="#000000"/>
  </RadioGroup>
 
  <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="18sp"
    android:textColor="#000000"
    android:text="这是竖着放的RadioGroup"/>
 
  <RadioGroup
    android:id="@+id/rg_vertical_demo"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
 
    <RadioButton
      android:layout_width="wrap_content"
      android:layout_height="0dp"
      android:layout_weight="1"
      android:checked="false"
      android:text="好"
      android:textSize="18sp"
      android:id="@+id/rb_vertical_good"
      android:textColor="#000000"/>
 
    <RadioButton
      android:layout_width="wrap_content"
      android:layout_height="0dp"
      android:layout_weight="1"
      android:checked="false"
      android:text="很好"
      android:textSize="18sp"
      android:id="@+id/rb_vertical_very_good"
      android:textColor="#000000"/>
  </RadioGroup>
 
  <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="18sp"
    android:textColor="#000000"
    android:text="这是改了图标竖着放的RadioGroup"/>
 
  <RadioGroup
    android:id="@+id/rg_vertical_custom_demo"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
 
    <RadioButton
      android:button="@drawable/radio_button_selector"
      android:layout_width="wrap_content"
      android:layout_height="0dp"
      android:layout_weight="1"
      android:checked="false"
      android:text="这个是直接设置button的RadioButton"
      android:textSize="18sp"
      android:id="@+id/rb_vertical_custom_good"
      android:textColor="#000000"/>
 
    <RadioButton
      android:button="@null"
      android:drawableLeft="@drawable/radio_button_selector"
      android:drawablePadding="10dp"
      android:layout_width="wrap_content"
      android:layout_height="0dp"
      android:layout_weight="1"
      android:checked="false"
      android:text="这个是设置drawableLeft属性的RadioButton"
      android:textSize="18sp"
      android:id="@+id/rb_vertical_custom_very_good"
      android:textColor="#000000"/>
  </RadioGroup>
</LinearLayout>

RadioGroupActivity.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package xyz.strasae.androidlearn.my;
 
import androidx.appcompat.app.AppCompatActivity;
 
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
 
public class RadioGroupActivity extends AppCompatActivity {
  RadioGroup rg_horizontal_demo;
  RadioGroup rg_vertical_demo;
 
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_radio_group);
    rg_horizontal_demo = findViewById(R.id.rg_horizontal_demo);
    rg_vertical_demo = findViewById(R.id.rg_vertical_demo);
    rg_horizontal_demo.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
      @Override
      public void onCheckedChanged(RadioGroup radioGroup, int i) {
        RadioButton rb_temp = findViewById(radioGroup.getCheckedRadioButtonId());
        Toast.makeText(RadioGroupActivity.this, String.format("你选择了%s", rb_temp.getText().toString()), Toast.LENGTH_SHORT).show();
      }
    });
    rg_vertical_demo.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
      @Override
      public void onCheckedChanged(RadioGroup radioGroup, int i) {
        RadioButton rb_temp = findViewById(radioGroup.getCheckedRadioButtonId());
        Toast.makeText(RadioGroupActivity.this, String.format("你选择了%s", rb_temp.getText().toString()), Toast.LENGTH_SHORT).show();
      }
    });
  }
}

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

原文链接:https://blog.csdn.net/weixin_43219615/article/details/98867869

延伸 · 阅读

精彩推荐
  • Android编写简易Android天气应用的代码示例

    编写简易Android天气应用的代码示例

    这篇文章主要介绍了编写简易Android天气应用的代码示例,文中的例子主要是利用到了RxAndroid处理异步方法,需要的朋友可以参考下...

    喝醉的毛毛虫6132021-06-17
  • Android如何在WorkManager中处理异步任务详解

    如何在WorkManager中处理异步任务详解

    这篇文章主要给大家介绍了关于如何在WorkManager中处理异步任务的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学...

    湫水长天10402022-09-24
  • Androidandroid播放gif格式图片示例

    android播放gif格式图片示例

    这篇文章主要介绍了android播放gif格式图片的方法,大家参考使用吧...

    Android开发网8792021-02-21
  • Androidandroid应用签名详细步骤

    android应用签名详细步骤

    这篇文章主要介绍了android应用签名详细步骤,需要的朋友可以参考下...

    Android开发网6972021-02-27
  • AndroidAndroid Native 内存泄漏系统化解决方案

    Android Native 内存泄漏系统化解决方案

    这篇文章主要介绍了Android Native 内存泄漏系统化解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友...

    高德技术4502022-10-25
  • AndroidAndroid编程实现图片拍照剪裁的方法

    Android编程实现图片拍照剪裁的方法

    这篇文章主要介绍了Android编程实现图片拍照剪裁的方法,涉及Android调用裁剪工具操作图片的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...

    wiseideal6732021-04-18
  • AndroidEmoji表情在Android JNI中的兼容性问题详解

    Emoji表情在Android JNI中的兼容性问题详解

    这篇文章主要给大家介绍了关于Emoji表情在Android JNI中的兼容性问题,文中通过示例代码介绍的非常详细,对大家学习或者使用Android JNI具有一定的参考学习...

    Darkness4633872022-10-31
  • AndroidListview加载的性能优化是如何实现的

    Listview加载的性能优化是如何实现的

    在android开发中Listview是一个很重要的组件,它以列表的形式根据数据的长自适应展示具体内容,用户可以自由的定义listview每一列的布局,接下来通过本文给...

    GoAgent6622021-05-20