Android编程单选项框RadioGroup综合应用示例
作者:Android_Tutor 时间:2022-01-12 19:16:29
本文实例讲述了Android编程单选项框RadioGroup用法。分享给大家供大家参考,具体如下:
今天介绍的是RadioGroup 的组事件.RadioGroup 可将各自不同的RadioButton ,设限于同一个Radio 按钮组,同一个RadioGroup 组里的按钮,只能做出单一选择(单选题).
首先,我们先设计一个TextView Widget ,以及一个RadioGroup ,并将该RadioGroup 内放置两个RadioButton ,默认为都不选择,在程序运行阶段,利用onCheckedChanged 作为启动事件装置,让User选择其中一个按钮,显示被选择的内容,最的将RadioButton 的选项文字显示于TextView 当中.
下面我们看一下效果图:
下面是涉及的相关代码:
string.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, RadioGroupDemo</string>
<string name="app_name">RadioGroupDemo</string>
<string name="tr_radio_op1">帅哥</string>
<string name="tr_radio_op2">美女</string>
<string name="str_radio_question1">请问你是?</string>
</resources>
主布局main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!--第一個TextView -->
<TextView
android:id="@+id/myTextView"
android:layout_width="228px"
android:layout_height="49px"
android:text="@string/str_radio_question1"
android:textSize="30sp"
/>
<!--建立一個RadioGroup -->
<RadioGroup
android:id="@+id/myRadioGroup"
android:layout_width="137px"
android:layout_height="216px"
android:orientation="vertical"
>
<!--第一個RadioButton -->
<RadioButton
android:id="@+id/myRadioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tr_radio_op1"
/>
<!--第二個RadioButton -->
<RadioButton
android:id="@+id/myRadioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tr_radio_op2"
/>
</RadioGroup>
</LinearLayout>
最后是主控制程序RadioGroupDemo.Java:
package com.android.test;
import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class RadioGroupDemo extends Activity
{
public TextView mTextView1;
public RadioGroup mRadioGroup1;
public RadioButton mRadio1,mRadio2;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/*取得 TextView、RadioGroup、RadioButton对象*/
mTextView1 = (TextView) findViewById(R.id.myTextView);
mRadioGroup1 = (RadioGroup) findViewById(R.id.myRadioGroup);
mRadio1 = (RadioButton) findViewById(R.id.myRadioButton1);
mRadio2 = (RadioButton) findViewById(R.id.myRadioButton2);
/*RadioGroup用OnCheckedChangeListener来运行*/
mRadioGroup1.setOnCheckedChangeListener(mChangeRadio);
}
private RadioGroup.OnCheckedChangeListener mChangeRadio = new
RadioGroup.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
// TODO Auto-generated method stub
if(checkedId==mRadio1.getId())
{
/*把mRadio1的内容传到mTextView1*/
mTextView1.setText(mRadio1.getText());
}
else if(checkedId==mRadio2.getId())
{
/*把mRadio2的内容传到mTextView1*/
mTextView1.setText(mRadio2.getText());
}
}
};
}
运行RadioGroupDemo.java ,将得到以上效果。
希望本文所述对大家Android程序设计有所帮助。
标签:Android,RadioGroup
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
MybatisPlus使用Wrapper实现条件查询功能
2021-11-29 10:21:08
![](https://img.aspxhome.com/file/2023/2/79322_0s.png)
Mybatis实现增删改查
2022-05-26 22:00:20
![](https://img.aspxhome.com/file/2023/9/65729_0s.png)
Java环境配置原理全面解析
2023-11-23 07:56:48
![](https://img.aspxhome.com/file/2023/5/59925_0s.jpg)
Spring轻松解决循环依赖
2021-11-07 15:06:11
![](https://img.aspxhome.com/file/2023/6/79216_0s.png)
IP查询系统的异步回调案例
2023-11-10 18:22:24
详解Java包装类及自动装箱拆箱
2023-11-14 21:13:23
Hadoop源码分析六启动文件namenode原理详解
2021-08-20 01:03:55
![](https://img.aspxhome.com/file/2023/9/62669_0s.png)
C#实现XML文件操作详解
2023-07-16 12:36:52
![](https://img.aspxhome.com/file/2023/0/67050_0s.png)
synchronized及JUC显式locks 使用原理解析
2023-08-05 03:28:41
Ajax实现省市区三级联动
2023-01-14 05:09:58
![](https://img.aspxhome.com/file/2023/3/71663_0s.png)
JAVA发送HTTP请求的多种方式详细总结
2021-06-15 14:43:48
JavaWeb实现文件上传与下载的方法
2023-12-23 04:42:56
SpringBoot Security权限控制自定义failureHandler实例
2022-12-03 08:46:58
C++调用C#的DLL程序实现方法
2023-02-04 13:58:16
Feign调用可重试的最佳方案分享
2021-06-09 12:19:23
详解JAVA 线程-线程的状态有哪些?它是如何工作的?
2023-11-27 03:33:09
![](https://img.aspxhome.com/file/2023/1/65181_0s.png)
解决使用mybatis-plus时,生成的SQL大写变小写加下划线问题
2022-10-20 09:24:47
Mybatis配置之typeAlias标签的用法
2023-11-27 20:18:20
![](https://img.aspxhome.com/file/2023/6/60446_0s.jpg)
C# winfrom 模拟ftp文件管理实现代码
2023-07-15 16:29:48
SpringMVC接收复杂集合对象(参数)代码示例
2023-01-29 18:33:51