当前位置:网站首页>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
边栏推荐
- 洞察——风格注意力网络(SANet)在任意风格迁移中的应用
- 汇编函数mcall systemstack asmcgocall syscall
- 栈-括号的匹配
- GrowingIO 响应式编程探索和实践
- 聊一聊数据库中的锁
- If you want to forget the WiFi network you used to connect to your Mac, try this!
- 华为HCIA笔记
- Bgfx compilation tutorial
- 在 Amazon SageMaker 管道模式下使用 Horovod 实现多 GPU 分布式训练
- In the age of screen reading, we suffer from attention deficit syndrome
猜你喜欢

Exception calling 'downloadstring' with '1' arguments: 'operation timed out'

从技术谈到管理,把系统优化的技术用到企业管理

On the coverage technology and best practice of go code

来自不同行业领域的50多个对象检测数据集

编程界大佬教你:一行Python代码能做出哪些神奇的事情?

聊聊Go代码覆盖率技术与最佳实践

From technology to management, the technology of system optimization is applied to enterprise management

Summary of the resumption of a 618 promotion project

A detailed explanation of microservice architecture

Web API series (3) unified exception handling
随机推荐
Business facade and business rule
小熊派开发板实践:智慧路灯沙箱实验之真实设备接入
Exception calling 'downloadstring' with '1' arguments: 'operation timed out'
Huawei HCIA notes
[漫谈] 软件设计的目标和途径
If you want to forget the WiFi network you used to connect to your Mac, try this!
CPU瞒着内存竟干出这种事
Vscode configuration
PHP security: the past and present of variables
高级并发编程系列九(Lock接口分析)
On the coverage technology and best practice of go code
Kubernetes服务类型浅析:从概念到实践
Design pattern of facade and mediator
聊一聊数据库中的锁
Using pipe() to improve code readability in pandas
What kind of technical ability should a programmer who has worked for 1-3 years? How to improve?
PHP安全:变量的前世今生
Stack bracket matching
在 Amazon SageMaker 管道模式下使用 Horovod 实现多 GPU 分布式训练
模型预测准确率高达94%!利用机器学习完美解决2000亿美元库存难题