RadioButton实现多选一

RadioButton实现多选一

大家好,又见面了,我是全栈君。

RadioButton实现多选一

一、简介

RadioButton实现多选一

 

二、RadioButton实现多选一方法

1、将多个RadioButton放在一个RadioGroup里面

 1     <RadioGroup  2         android:id="@+id/radioGroup1"
 3         android:layout_width="match_parent"
 4         android:layout_height="wrap_content" >
 5 
 6         <RadioButton
 7             android:layout_width="wrap_content"
 8             android:layout_height="wrap_content"
 9             android:text="男"
10             android:textColor="#FFFFFF" />
11 
12         <RadioButton
13             android:layout_width="wrap_content"
14             android:layout_height="wrap_content"
15             android:text="女"
16             android:textColor="#FFFFFF" />
17     </RadioGroup>

 

2、在RadioGroup里面取出每个RadioButton

 1 public void onClick(View v) {
 2                 // TODO Auto-generated method stub
 3                 int len = radioGroup1.getChildCount();
 4                 for (int i = 0; i < len; i++) {
 5                     RadioButton radio = (RadioButton) radioGroup1.getChildAt(i);11                 }
12             }

 

3、检查每个RadioButton是否被选取

1 if (radio.isChecked()) {
    
    

4 break; 5 }

 

4、取出被选取的那个RadioButton里面的值

1 Toast.makeText(Activity01.this, radio.getText(),
2                                 Toast.LENGTH_LONG).show();

 

 

三、代码实例

效果图:

RadioButton实现多选一

 

代码:

fry.Activity01

 1 package fry;
 2 
 3 import com.example.RadioButtonDemo1.R;
 4 
 5 import android.app.Activity;
 6 import android.os.Bundle;
 7 import android.view.View;
 8 import android.view.View.OnClickListener;
 9 import android.widget.Button;
10 import android.widget.RadioButton;
11 import android.widget.RadioGroup;
12 import android.widget.TextView;
13 import android.widget.Toast;
14 
15 public class Activity01 extends Activity {
16     private Button btn_chooseGender;
17     private RadioGroup radioGroup1;
18     private TextView tv_answer;
19 
20     @Override
21     protected void onCreate(Bundle savedInstanceState) {
22         // TODO Auto-generated method stub
23         super.onCreate(savedInstanceState);
24         setContentView(R.layout.activity01);
25 
26         btn_chooseGender = (Button) findViewById(R.id.btn_chooseGender);
27         radioGroup1 = (RadioGroup) findViewById(R.id.radioGroup1);
28         tv_answer = (TextView) findViewById(R.id.tv_answer);
29         /*
30          * RadioButton实现多选一方法
31          * 1、将多个RadioButton放在一个RadioGroup里面
32          * 2、在RadioGroup里面取出每个RadioButton 
33          * 3、检查每个RadioButton是否被选取
34          * 4、取出被选取的那个RadioButton里面的值
35          */
36         btn_chooseGender.setOnClickListener(new OnClickListener() {
37 
38             @Override
39             public void onClick(View v) {
40                 // TODO Auto-generated method stub
41                 int len = radioGroup1.getChildCount();
42                 for (int i = 0; i < len; i++) {
43                     RadioButton radio = (RadioButton) radioGroup1.getChildAt(i);
44                     if (radio.isChecked()) {
45                         Toast.makeText(Activity01.this, radio.getText(),
46                                 Toast.LENGTH_LONG).show();
47                         break;
48                     }
49                 }
50             }
51         });
52     }
53 }

/RadioButtonDemo1/res/layout/activity01.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:background="@android:color/black"
 6     android:orientation="vertical" >
 7 
 8     <TextView
 9         android:layout_width="match_parent"
10         android:layout_height="wrap_content"
11         android:text="性别"
12         android:textAppearance="?android:attr/textAppearanceLarge"
13         android:layout_gravity="center_horizontal"
14         android:textColor="#FFFFFF" />
15 
16     <RadioGroup
17         android:id="@+id/radioGroup1"
18         android:layout_width="match_parent"
19         android:layout_height="wrap_content" >
20 
21         <RadioButton
22             android:layout_width="wrap_content"
23             android:layout_height="wrap_content"
24             android:text="男"
25             android:textColor="#FFFFFF" />
26 
27         <RadioButton
28             android:layout_width="wrap_content"
29             android:layout_height="wrap_content"
30             android:text="女"
31             android:textColor="#FFFFFF" />
32     </RadioGroup>
33 
34     <Button 
35         android:id="@+id/btn_chooseGender"
36         android:layout_width="match_parent"
37         android:layout_height="wrap_content"
38         android:text="选择性别"
39         android:textColor="#FFFFFF" />
40         />
41         
42     <TextView
43         android:id="@+id/tv_answer"
44         android:layout_width="match_parent"
45         android:layout_height="wrap_content"
46         android:text=""
47         android:textAppearance="?android:attr/textAppearanceLarge"
48         android:layout_gravity="center_horizontal"
49         android:textColor="#FFFFFF" />
50 </LinearLayout>

 

四、收获

1、

android:textColor=”#FFFFFF”

设置颜色,直接用#FFFFFF

2、

android:layout_gravity=”center_horizontal”

文字居中显示

3、

RadioButton在RadioGroup里面实现多选一

4、

android:background=”@android:color/black”

设置黑色,系统自带颜色

5、

int len = radioGroup1.getChildCount();

RadioGroup获取孩子数量

6、

RadioButton radio = (RadioButton) radioGroup1.getChildAt(i);

RadioGroup获取孩子

7、

if (radio.isChecked())

判断RadioButton是否被选取

8、

Toast.makeText(Activity01.this, radio.getText(),Toast.LENGTH_LONG).show();

吐司

 

转载于:https://www.cnblogs.com/Renyi-Fan/p/7291194.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/108269.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • JAVA大数据后台管理系统

    JAVA大数据后台管理系统一款Java语言基于SpringBoot2.x、Layui、Thymeleaf、MybatisPlus、Shiro、MySQL等框架精心打造的一款模块化、插件化、高性能的前后端分离架构敏捷开发框架,可用于快速搭建前后端分离后台管理系统,本着简化开发、提升开发效率的初衷,自研了一套个性化的组件,实现了可插拔的组件式开发方式:单图上传、多图上传、下拉选择、开关按钮、单选按钮、多选按钮、图片裁剪等

    2022年5月4日
    58
  • Antd的table筛选,表头columns的filters过滤清空

    Antd的table筛选,表头columns的filters过滤清空Form+Table实现了自定义筛选菜单的功能。具体可以参考https://ant.design/components/table-cn/#components-table-demo-custom-filter-panel。但是此功能会有bug:选择相应的搜索条件后,点击“搜索”按钮,Table会渲染相应的数据,且Table表头也有自带的过滤功能(实际上是column的filters属性起的作用);然后再点击“清除”按钮,所有的搜索条件和表头里filters过滤的条件都要被清除。但是Ta.

    2022年5月21日
    51
  • SQL游标

    SQL游标游标(MSSQL)例子:银行取钱1000块钱方案:1ATM点击取款100010张2ATM点击取款100取10次遍历思想优点:允许你一个个的遍历缺点:效率非常的低注意:一般情况下,不要

    2022年7月4日
    25
  • 计算机测试英语词汇,英语听说测试-计算机专业英语词汇.pdf「建议收藏」

    英语听说测试-计算机专业英语词汇常用计算机专业英语1.filen.文件;v.保存文件43.enterv.键入,送入2.commandn.命令,指令44.marginn.余量,边缘,边际3.usev.使用,用途…

    2022年4月17日
    36
  • python垃圾回收机制原理

    python垃圾回收机制原理#python垃圾回收机制详解一、概述:  python的GC模块主要运用了“引用计数(referencecounting)”来跟踪和回收垃圾。在引用计数的基础上,还可以通过标记清除(markandsweep)解决容器(这里的容器值指的不是docker,而是数组,字典,元组这样的对象)对象可能产生的循环引用的问题。通过“分代回收(generationcollection)”以空间换取时间来进一步提高垃圾回收的效率。二、垃圾回收三种机制  1、引用计数  在Python中,大多数对象的生命周

    2022年6月24日
    32
  • UNet详解(附图文和代码实现)

    卷积神经网络被大规模的应用在分类任务中,输出的结果是整个图像的类标签。但是UNet是像素级分类,输出的则是每个像素点的类别,且不同类别的像素会显示不同颜色,UNet常常用在生物医学图像上,而该任务中图片数据往往较少。所以,Ciresan等人训练了一个卷积神经网络,用滑动窗口提供像素的周围区域(patch)作为输入来预测每个像素的类标签。这个网络有两个优点:(1)输出结果可以定位出目标类别的位置;(2)由于输入的训练数据是patches,这样就相当于进行了数据增强,从而解决了生物医学图像数量少的问题。但是,

    2022年4月4日
    226

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号