当前位置:网站首页>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
边栏推荐
猜你喜欢
Huawei HCIA notes
Ac86u KX Online
Insight -- the application of sanet in arbitrary style transfer
Using thread communication to solve the problem of cache penetrating database avalanche
Advanced concurrent programming series 9 (lock interface analysis)
Annual salary of 900000 programmers is not as good as 3800 civil servants a month? How to choose between stability and high income?
Git代码提交操作,以及git push提示failed to push some refs'XXX'
微服务的出现和意义的探索
Analysis of kubernetes service types: from concept to practice
What should be considered in the promotion plan outside the station?
随机推荐
盘点那些争议最大的编程观点,你是什么看法呢?
Summary of the resumption of a 618 promotion project
DOM node operation
某618大促项目的复盘总结
Reflection on a case of bus card being stolen and swiped
京淘项目day09
模型预测准确率高达94%!利用机器学习完美解决2000亿美元库存难题
chrome浏览器跨域Cookie的SameSite问题导致访问iframe内嵌页面异常
C language I blog assignment 03
Vscode configuration
MongoDB下,启动服务时,出现“服务没有响应控制功能”解决方法
小熊派开发板实践:智慧路灯沙箱实验之真实设备接入
不要把异常当做业务逻辑,这性能可能你无法承受
On the coverage technology and best practice of go code
你可能不知道的Animation动画技巧与细节
在pandas中使用pipe()提升代码可读性
PHP security: the past and present of variables
How to learn technology efficiently
C语言Ⅰ博客作业03
高级并发编程系列九(Lock接口分析)