当前位置:网站首页>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
边栏推荐
- From technology to management, the technology of system optimization is applied to enterprise management
- Why do we need software engineering -- looking at a simple project
- 凯撒密码实现
- Using thread communication to solve the problem of cache penetrating database avalanche
- How did I lose control of the team?
- 如何高效的学习技术
- The prediction accuracy of the model is as high as 94%! Using machine learning to solve the 200 billion dollar inventory problem perfectly
- Share several vs Code plug-ins I use everyday
- 如何以计算机的方式去思考
- Exception calling 'downloadstring' with '1' arguments: 'operation timed out'
猜你喜欢
随机推荐
Summary of the resumption of a 618 promotion project
阿里terway源码分析
A detailed explanation of microservice architecture
There's not much time left for Kwai Chung.
The CPU does this without the memory
If you want to forget the WiFi network you used to connect to your Mac, try this!
洞察——风格注意力网络(SANet)在任意风格迁移中的应用
不要把异常当做业务逻辑,这性能可能你无法承受
深入web workers (上)
华为HCIA笔记
Git代码提交操作,以及git push提示failed to push some refs'XXX'
Adobe Prelude /Pl 2020软件安装包(附安装教程)
一万四千字分布式事务原理解析,全部掌握你还怕面试被问?
CPU瞒着内存竟干出这种事
Annual salary of 900000 programmers is not as good as 3800 civil servants a month? How to choose between stability and high income?
Don't treat exceptions as business logic, which you can't afford
Why do we need software engineering -- looking at a simple project
技术总监7年自述——如何选择一家好公司
大数据算法——布隆过滤器
聊聊Go代码覆盖率技术与最佳实践