Android程序开发中单选按钮(RadioGroup)的使用详解
作者:mrr 时间:2023-09-18 03:43:39
在还没给大家介绍单选按钮(RadioGroup)的使用,先给大家展示下效果图吧:
xml文件
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:id="@+id/txt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="您的性别为"/>
<RadioGroup
android:id="@+id/sex"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/male"
android:text="男"/>
<RadioButton
android:id="@+id/female"
android:text="女"/>
</RadioGroup>
</LinearLayout>
java文件
public class
MainActivity extends Activity {
private TextView txt=null;
private RadioGroup sex=null;
private RadioButton male=null;
private RadioButton female=null;
@Override
protected void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.txt=(TextView)
super.findViewById(R.id.txt);
this.sex=(RadioGroup)
super.findViewById(R.id.sex);
this.male=(RadioButton)
super.findViewById(R.id.male);
this.female=(RadioButton)
super.findViewById(R.id.female);
this.sex.setOnCheckedChangeListener(new
OnCheckedChangeListenerImp());
} private class
OnCheckedChangeListenerImp implements
OnCheckedChangeListener{
public void
onCheckedChanged(RadioGroup group, int checkedId)
{ String temp=null;
if(MainActivity.this.male.getId()==checkedId){
temp="男";
} else if(MainActivity.this.female.getId()==checkedId){
temp="女";
} MainActivity.this.txt.setText("您的性别是"+temp);
} }
以上所述是小编给大家介绍的Android程序开发中单选按钮(RadioGroup)的使用详解,希望对大家有所帮助!
标签:android,单选,按钮
0
投稿
猜你喜欢
浅谈BeanPostProcessor加载次序及其对Bean造成的影响分析
2022-05-02 19:52:29
Mybatis-Plus sum聚合函数及按日期查询并求和的方式详解
2022-07-09 12:58:07
java新特性之for循环最全的用法总结
2022-07-08 22:14:51
Java中比较运算符compareTo()、equals()与==的区别及应用总结
2023-11-28 20:08:28
wpf将表中数据显示到datagrid示例
2023-06-13 04:14:06
Redis在springboot中的使用教程
2021-10-02 18:10:46
Java Spring框架的注解式开发你了解吗
2022-12-14 23:43:18
Android使用元数据实现配置信息的传递方法详细介绍
2023-11-02 15:25:48
C#中Foreach循环遍历的本质与枚举器详解
2022-08-04 05:31:12
Flutter封装组动画混合动画AnimatedGroup示例详解
2022-12-29 17:17:05
浅析Spring Boot单体应用熔断技术的使用
2022-05-10 02:37:08
Android 多国语言value文件夹命名的方法
2022-04-19 00:43:40
Java多线程回调方法实例解析
2023-11-04 01:40:01
初识Spring Boot框架之Spring Boot的自动配置
2022-08-25 10:27:57
java输入字符串并将每个字符输出的方法
2022-10-04 01:25:37
java多线程编程实例
2022-12-08 18:51:29
Android序列化接口Parcelable与Serializable接口对比
2023-03-24 17:48:59
Android提高之使用NDK把彩图转换灰度图的方法
2023-07-22 02:00:07
JavaFx Tooltip悬浮提示使用及自定义代码详解
2023-05-11 15:06:05
JavaSwing FlowLayout 流式布局的实现
2023-10-02 03:59:41