当前位置:网站首页>android基础-RadioButton(单选按钮)
android基础-RadioButton(单选按钮)
2020-11-07 20:58:00 【ZHAO_JH】
==*在这里开始引用事件处理,需要先看博文《android基础-事件处理》==
简介
RadioButton 单选按钮放置在 RadioGroup 中使用,在后端获取布局页面选择项目数据
| RadioButton 对象方法 | 释义 |
|---|---|
| getChildCount( ) | 获得按钮组中的单选按钮的数目; |
| getChinldAt(i) | 根据索引值获取我们的单选按钮 |
| isChecked( ) | 判断按钮是否选中 |
| getText() | 获取值 |
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请选择性别"
android:textSize="23dp"
/>
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/btnMan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"
android:checked="true"/>
<RadioButton
android:id="@+id/btnWoman"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"/>
</RadioGroup>
<Button
android:id="@+id/btnpost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="myclick"
android:text="提交"/>
</LinearLayout>
监听按钮组ID选择触发
MainActivity.java
package com.example.myapplication;
import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// 保存Activity的状态
super.onCreate(savedInstanceState);
// 加载视图
setContentView(R.layout.activity_main);
// 获取单选按钮组对象
RadioGroup radgroup = (RadioGroup) findViewById(R.id.radioGroup);
// 监听按钮
radgroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
// 单选按钮组对象当做参数传入,以便获取单选对象
public void onCheckedChanged(RadioGroup group, int checkedId) {
// 获取单选对象
RadioButton radbtn = (RadioButton) findViewById(checkedId);
Toast.makeText(getApplicationContext(), "按钮组值发生改变,你选了" + radbtn.getText(), Toast.LENGTH_LONG).show();
}
});
}
}
onClick 提交触发
package com.example.myapplication;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// 保存Activity的状态
super.onCreate(savedInstanceState);
// 加载视图
setContentView(R.layout.activity_main);
}
public void myclick(View source)
{
// 获取单选按钮组对象
RadioGroup radgroup = (RadioGroup) findViewById(R.id.radioGroup);
// 遍历单选按钮组中对象,找到有checked属性的对象(即找到选中的对象)
for (int i = 0; i < radgroup.getChildCount(); i++) {
RadioButton rd = (RadioButton) radgroup.getChildAt(i);
if (rd.isChecked()) {
Toast.makeText(getApplicationContext(), "点击提交按钮,获取你选择的是:" + rd.getText(), Toast.LENGTH_LONG).show();
break;
}
}
}
}
版权声明
本文为[ZHAO_JH]所创,转载请带上原文链接,感谢
https://my.oschina.net/zhaojunhui/blog/4707702
边栏推荐
- 盘点那些争议最大的编程观点,你是什么看法呢?
- 如何高效的学习技术
- [C + + learning notes] how about the simple use of the C + + standard library STD:: thread?
- Facebook开源框架如何简化 PyTorch 实验
- 小熊派开发板实践:智慧路灯沙箱实验之真实设备接入
- DOM节点操作
- DOM节点操作
- In the age of screen reading, we suffer from attention deficit syndrome
- Git code submission operation, and git push prompt failed to push some refs'xxx '
- Win10官方1909版本无法打开windows安全中心中病毒和威胁防护的实时保护解决方案。
猜你喜欢

一次公交卡被“盜刷”事件帶來的思考

Kubernetes服务类型浅析:从概念到实践

The JS solution cannot be executed after Ajax loads HTML

华为HCIA笔记

On the coverage technology and best practice of go code

洞察——风格注意力网络(SANet)在任意风格迁移中的应用

Reflection on a case of bus card being stolen and swiped

Using pipe() to improve code readability in pandas

Ac86u KX Online

Git code submission operation, and git push prompt failed to push some refs'xxx '
随机推荐
How Facebook open source framework simplifies pytorch experiment
ajax 载入html后不能执行其中的js解决方法
A detailed explanation of microservice architecture
凯撒密码实现
全网最硬核讲解计算机启动流程
The prediction accuracy of the model is as high as 94%! Using machine learning to solve the 200 billion dollar inventory problem perfectly
What is the relationship between low code vs model driven?
关于update操作并发问题
深入web workers (上)
[random talk] the goal and way of software design
Git code submission operation, and git push prompt failed to push some refs'xxx '
It's time to end bertology
在pandas中使用pipe()提升代码可读性
Summary of the resumption of a 618 promotion project
从技术谈到管理,把系统优化的技术用到企业管理
Using pipe() to improve code readability in pandas
ECMAScript7规范中的instanceof操作符
【C++学习笔记】C++ 标准库 std::thread 的简单使用,一文搞定还不简单?
Principles of websocket + probuf
一万四千字分布式事务原理解析,全部掌握你还怕面试被问?