iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >Android中怎么利用RadioButton控件实现多选一功能
  • 883
分享到

Android中怎么利用RadioButton控件实现多选一功能

radiobuttonandroid 2023-05-30 22:05:52 883人浏览 八月长安
摘要

Android中怎么利用RadioButton控件实现多选一功能,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。将多个RadioButton放在一个RadioGroup里面<

Android中怎么利用RadioButton控件实现多选一功能,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

将多个RadioButton放在一个RadioGroup里面

<RadioGroup  android:id="@+id/radioGroup1"  android:layout_width="match_parent"  android:layout_height="wrap_content" >  <RadioButton   android:layout_width="wrap_content"   android:layout_height="wrap_content"   android:text="男"   android:textColor="#FFFFFF" />  <RadioButton   android:layout_width="wrap_content"   android:layout_height="wrap_content"   android:text="女"   android:textColor="#FFFFFF" /> </RadioGroup>

在RadioGroup里面取出每个RadioButton

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

检查每个RadioButton是否被选取

 if (radio.isChecked()) {      break;     }

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

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

 三、代码实例

效果图:

 代码:

fry.Activity01

package fry;import com.example.RadioButtonDemo1.R;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.TextView;import android.widget.Toast;public class Activity01 extends Activity { private Button btn_chooseGender; private RadioGroup radioGroup1; private TextView tv_answer; @Override protected void onCreate(Bundle savedInstanceState) {  // TODO Auto-generated method stub  super.onCreate(savedInstanceState);  setContentView(R.layout.activity01);  btn_chooseGender = (Button) findViewById(R.id.btn_chooseGender);  radioGroup1 = (RadioGroup) findViewById(R.id.radioGroup1);  tv_answer = (TextView) findViewById(R.id.tv_answer);    btn_chooseGender.setOnClickListener(new OnClickListener() {   @Override   public void onClick(View v) {    // TODO Auto-generated method stub    int len = radioGroup1.getChildCount();    for (int i = 0; i < len; i++) {     RadioButton radio = (RadioButton) radioGroup1.getChildAt(i);     if (radio.isChecked()) {      Toast.makeText(Activity01.this, radio.getText(),        Toast.LENGTH_LONG).show();      break;     }    }   }  }); }}

/RadioButtonDemo1/res/layout/activity01.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="Http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/black" android:orientation="vertical" > <TextView  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:text="性别"  android:textAppearance="?android:attr/textAppearanceLarge"  android:layout_gravity="center_horizontal"  android:textColor="#FFFFFF" /> <RadioGroup  android:id="@+id/radioGroup1"  android:layout_width="match_parent"  android:layout_height="wrap_content" >  <RadioButton   android:layout_width="wrap_content"   android:layout_height="wrap_content"   android:text="男"   android:textColor="#FFFFFF" />  <RadioButton   android:layout_width="wrap_content"   android:layout_height="wrap_content"   android:text="女"   android:textColor="#FFFFFF" /> </RadioGroup> <Button   android:id="@+id/btn_chooseGender"  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:text="选择性别"  android:textColor="#FFFFFF" />  />   <TextView  android:id="@+id/tv_answer"  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:text=""  android:textAppearance="?android:attr/textAppearanceLarge"  android:layout_gravity="center_horizontal"  android:textColor="#FFFFFF" /></LinearLayout>

四、收获

android:textColor="#FFFFFF"

设置颜色,直接用#FFFFFF

android:layout_gravity="center_horizontal"

文字居中显示

RadioButton在RadioGroup里面实现多选一

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

设置黑色,系统自带颜色

int len = radioGroup1.getChildCount();

RadioGroup获取孩子数量

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

RadioGroup获取孩子

if (radio.isChecked())

判断RadioButton是否被选取

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

看完上述内容,你们掌握Android中怎么利用RadioButton控件实现多选一功能的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注编程网精选频道,感谢各位的阅读!

--结束END--

本文标题: Android中怎么利用RadioButton控件实现多选一功能

本文链接: https://www.lsjlt.com/news/221748.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • c#程序自启动怎么设置
    c# 程序的自启动方法有三种:注册表:在指定注册表项下创建新值,并将其设置为程序可执行文件路径。任务计划程序:创建一个新任务,并在触发器和动作部分分别指定登录时或特定时间触发,以及启动程...
    99+
    2024-05-14
    c#
  • c#怎么调用dll文件
    可在 c# 中轻松调用 dll 文件:引用 dll(使用 dllimport 特性)定义与 dll 函数签名匹配的函数原型调用 dll 函数(如同 c# 函数)附加技巧:使用 chars...
    99+
    2024-05-14
    c#
  • 如何构建 Golang RESTful API,并实现 CRUD 操作?
    通过创建 golang 项目并安装必要的包,我们可以构建一个功能齐全的 restful api。它使用 mysql 数据库进行 crud 操作:1. 创建和连接数据库;2. 定义数据结构...
    99+
    2024-05-14
    go crud mysql git golang
  • c#怎么添加类文件
    在c#中添加类文件的步骤:1. 创建新项目,2. 添加新类,3. 为类添加代码,4. 在另一个类中引用新类。using语句引用类文件所在的命名空间;new运算符创建类的新实例;点运算符访...
    99+
    2024-05-14
    c#
  • 使用 C++ 构建高性能服务器架构的最佳实践
    遵循 c++++ 中构建高性能服务器架构的最佳实践可以创建可扩展、可靠且可维护的系统:使用线程池以重用线程,提高性能。利用协程减少上下文切换和内存开销,提升性能。通过智能指针和引用计数优...
    99+
    2024-05-14
    c++ 高性能服务器架构 数据访问
  • c#怎么添加字段
    在 c# 中添加字段包括以下步骤:声明字段:在类或结构中使用 字段类型 字段名; 语法声明字段。访问修饰符:用于限制对字段的访问,如 private、public、protected 和...
    99+
    2024-05-14
    c#
  • c#中怎么添加引用
    c# 中添加引用的方法有四种:使用 nuget 包管理器添加软件包。添加项目引用以包含其他项目。手动编辑项目文件 (.csproj) 以添加引用。从编译器命令行使用 /reference...
    99+
    2024-05-14
    c#
  • c#怎么创建文本文件
    在 c# 中创建文本文件的方法包括:创建 filestream 对象以打开或创建文件。使用 streamwriter 写入文本至文件。关闭 streamwriter 对象释放资源。关闭 ...
    99+
    2024-05-14
    c#
  • c#怎么定义属性
    如何在 c# 中定义属性 属性是一种编程构造,它包含一个 get 访问器和一个 set 访问器,允许以一种类属性的方式访问字段。它们提供了一种安全且封装的方式来访问和修改类的内部数据。 ...
    99+
    2024-05-14
    c#
  • 基于 C++ 的服务器架构的安全性考虑因素
    在设计基于 c++++ 的服务器架构时,安全考虑至关重要:使用 std::string 或 std::vector 避免缓冲区溢出。使用正则表达式或库函数验证用户输入。采用输出转义防止跨...
    99+
    2024-05-14
    安全性 关键词: c++ 服务器架构 c++ lsp
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作