android.widget.RadioButton 单选按钮(转)
大家好,我们今天这一节要介绍的是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.widget.RadioButton 单选按钮(转)的更多相关文章
- Android控件-单选按钮RadioButton
RadioGroup单选按钮用法,还是先看效果图 先中后,点RadioGroup测试按钮,可在标题栏显示选择结果,点清除可以清除选择.下面上代码,main.xml: <RadioGroup an ...
- 03 RadioButton 单选按钮
>概念:从多个互斥选项中选择一个 如果是选项全部展开 RadioButton 不是展开的Spinner(下拉列表) >属性: android:checked="tr ...
- 控件_RadioGroup&&RadioButton(单选按钮)和Toast
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools=&q ...
- Android之RadioButton多行
RadioGroup设置orientation="vertical"竖向单列显示 RadioGroup设置orientation="horizontal"横向单 ...
- android开发之单选按钮
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android=&q ...
- Android设置RadioButton在文字的右边
效果图如下: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro ...
- Android 自定义RadioButton实现
由于使用小米系统MIUI运行是RadioButton样式跟google Android API自定义的不一样,则我们可以定义任何想要的东东.没有做不到,只有想不到 Android 自定义RadioBu ...
- 关于java.lang.NoSuchMethodError: android.widget.RelativeLayout.setBackground的解决办法
今天用一个安卓4.0.4版本的手机测试手上的项目,发现logcat弹出这样一个提示“java.lang.NoSuchMethodError: android.widget.RelativeLayout ...
- java.lang.ClassCastException: android.view.AbsSavedState$1 cannot be cast to android.widget.ProgressBar$SavedState
java.lang.ClassCastException: android.view.AbsSavedState$1 cannot be cast to android.widget.Progress ...
随机推荐
- GDC2014免费slide整理下载
GDC2014图形和程序相关的部分免费slide整理下载:)http://pan.baidu.com/s/1eQzMrxo gdc14_An Indie Approach to procedural ...
- c# 靠谱的bitmap转byte[]
public static byte[] Bitmap2Byte(Bitmap bitmap) { using (MemoryStream stream = new MemoryStream()) { ...
- Flex Array 与 ArrayCollection.转自网络
1.array作为控件使用 FLEX3写法: <mx:Array id="barname"> <mx:String>Flash</mx:String& ...
- Tips12: 私人定制 专属的Unity3D 脚本模板
在使用U3D的过程中,新建一个C#脚本,它包含着空的Start()和Update()函数. 根据个人习惯的不同,可能有些人有着自己的脚本风格,每次进去都增删改很麻烦,这里介绍一个更改新建脚本模板的方 ...
- Transact-SQL 示例 - 使用脚本备份数据库的示例
在常规的数据库开发与维护的过程中,常常需要对数据库进行数据备份,最入门的办法就是使用SSMS图形化界面提供的数据库备份向导一步一步操作进行备份,这种方式虽然简单快捷但是日子久了就会觉得重复且繁琐.下面 ...
- 浅析I/O模型及其设计模式
前言 I/O在软件开发中的重要性无需多言,无论是在操作系统.网络协议.DBMS这种底层支撑软件还是在移动APP,大型网站服务器等应用软件的开发中都是最核心最重要的部分.特别是现在软件服务使用量和数据量 ...
- Sql [hierarchyid]类型如何动态插入层级数据
[hierarchyid] 是个不错的数据类型,能够方便的操作树型结构,网上找了很多资料没找到如何做到动态插入节点的例子,只好从MSDN认真看了下资料写出了一个DEMO CREATE TABLE Em ...
- C#语法糖之 cache操作类 asp.net
因为考虑到我下面我将写session cookies 等 操作类 ,与cache具有共性. 所以都统一继承了IHttpStorageObject abstract class 来保函数风格的统一 , ...
- 关于完整解答Leo C.W博客中名为“我们公司的ASP.NET 笔试题,你觉得难度如何”的所有题目
关于完整解答Leo C.W博客中名为“我们公司的ASP.NET 笔试题,你觉得难度如何”的所有题目,请大家鉴定,不足之处,敬请指教! 第1到3题解答如下: public enum QuestionTy ...
- 通过Nginx实现负载均衡
百度了下负载均衡:英文名称为Load Balance,其意思就是分摊到多个操作单元上进行执行,例如Web服务器.FTP服务器.企业关键应用服务器和其它关键任务服务器等,从而共同完成工作任务 嗯,跟我理 ...