当前位置:网站首页>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
边栏推荐
- Why do we need software engineering -- looking at a simple project
- How to think in the way of computer
- The official 1909 version of win10 cannot open the real-time protection solution of virus and threat protection in windows security center.
- laravel8更新之维护模式改进
- AFO
- AFO记
- Adobe Lightroom /Lr 2021软件安装包(附安装教程)
- On the coverage technology and best practice of go code
- Improvement of maintenance mode of laravel8 update
- Don't treat exceptions as business logic, which you can't afford
猜你喜欢
随机推荐
模型预测准确率高达94%!利用机器学习完美解决2000亿美元库存难题
大数据算法——布隆过滤器
阿里terway源码分析
计组-总线通信控制之异步串行通信的数据传输
C language I blog assignment 03
HandlerMethodArgumentResolver使用和原理
use Xunit.DependencyInjection Transformation test project
Don't treat exceptions as business logic, which you can't afford
The official 1909 version of win10 cannot open the real-time protection solution of virus and threat protection in windows security center.
AFO
是时候结束 BERTology了
Git code submission operation, and git push prompt failed to push some refs'xxx '
Adobe Prelude /Pl 2020软件安装包(附安装教程)
Share several vs Code plug-ins I use everyday
一万四千字分布式事务原理解析,全部掌握你还怕面试被问?
Count the frequency of letters in text (case insensitive)
DOM节点操作
一文详解微服务架构
What magic things can a line of Python code do?
How to choose a good company