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)
上一篇 2022年3月5日 下午9:00
下一篇 2022年3月5日 下午9:00


相关推荐

  • python字典由键插值_SciPyTutorial-一元插值interp1d

    python字典由键插值_SciPyTutorial-一元插值interp1d10.ScipyTutorial-插值interp1d插值,即依据一系列的点$(x_i,y_i)$通过一定的算法找到一个合适的函数来包含(逼近)这些点,反应出这些点的走势规律。interp1d。scipy.interpolate包里有很多的模块可以实现对一些已知的点进行插值,即找到一个合适的函数,例如模块interp1d。当得到插值函数后便可用这个插值函数计算其他$x_j$对应的的$y_j…

    2022年5月15日
    89
  • Python中的/与//的区别

    Python中的/与//的区别在github的项目中的水仙花例题中:1fornuminrange(100,1000):2low=num//103mid=num//10%104high=num//10

    2022年7月5日
    21
  • 见路不走

    见路不走

    2026年3月12日
    2
  • 移动端开发规范

    移动端开发规范移动端开发规范引言:最近得空,整理一些平时工作中要求的开发规范,浅薄之处还请大家多指教。目录移动端开发规范代码规范基本原则代码清晰一致性通用规范类命名方法命名变量命名常量命名枚举类型命名图片命名通用规范通用设计规范开屏页版本号版本检查开屏页广告推送通用测试用例及处理规范规范用例数据埋点规范…

    2022年6月24日
    34
  • 学习SyntaxHighlighter

    学习SyntaxHighlighter一款高亮显示各种格式的开源插件 下载地址 http alexgorbatch com SyntaxHighli download 下载后解压 demos 目录中有对应的例子 nbsp SyntaxHighli 的用法相对简单 官网原文如下 BasicStepsTo

    2026年3月17日
    2
  • 维吉尼亚密码原理详解及算法实现

    维吉尼亚密码原理详解及算法实现Playfair 密码编写 或者采用维吉尼亚密码编写 输入明文长度是任意的 明文 量子通信保密技术的诞生和快速发展主要取决于以下两个因素 a 经典保密通信面临着三个难以彻底解决的关键问题 即密钥协商 身份识别和窃听检测 这些问题的有效解决需要新技术 b 在对新技术的探索中 人们发现了量子内在的安全特性及其可能的应用 请写出你的密码机输出结果 可以采用 Playfair 密码或者 Vigen re 密码 这里我采用的是 Vigen recipher 完成信息加密 在 Vigener

    2026年3月19日
    3

发表回复

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

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