当前位置:网站首页>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
边栏推荐
- How to choose a good company
- Web API series (3) unified exception handling
- Implementation of multi GPU distributed training with horovod in Amazon sagemaker pipeline mode
- C language I blog assignment 03
- What do you think of the most controversial programming ideas?
- 如何高效的学习技术
- AC86U kx上网
- 高级并发编程系列九(Lock接口分析)
- 模型预测准确率高达94%!利用机器学习完美解决2000亿美元库存难题
- Design pattern of facade and mediator
猜你喜欢

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

Why do we need software engineering -- looking at a simple project

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

Advanced concurrent programming series 9 (lock interface analysis)

Git代码提交操作,以及git push提示failed to push some refs'XXX'

Win10官方1909版本无法打开windows安全中心中病毒和威胁防护的实时保护解决方案。

Design pattern of facade and mediator

Analysis of kubernetes service types: from concept to practice

Web API系列(三)统一异常处理

DOM node operation
随机推荐
Adobe media encoder /Me 2021软件安装包(附安装教程)
利用线程通信、解决缓存穿透数据库雪崩
ECMAScript7规范中的instanceof操作符
There's not much time left for Kwai Chung.
快速上手Git
聊一聊数据库中的锁
A detailed explanation of microservice architecture
当 TiDB 与 Flink 相结合:高效、易用的实时数仓
构造请求日志分析系统
GrowingIO 响应式编程探索和实践
How Facebook open source framework simplifies pytorch experiment
What magic things can a line of Python code do?
Summary of the resumption of a 618 promotion project
On hiz buffer
我是如何失去团队掌控的?
带你深入了解 GitLab CI/CD 原理及流程
分享几个我日常使用的VS Code插件
不懂数据库索引的底层原理?那是因为你心里没点b树
技术债务是对业务功能缺乏真正的理解 -daverupert.com
Annual salary of 900000 programmers is not as good as 3800 civil servants a month? How to choose between stability and high income?