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 ...
随机推荐
- mac word 快捷键
https://support.office.com/en-us/article/Word-keyboard-shortcuts-c0ca851f-3d58-4ce0-9867-799df7366 ...
- ODAC(V9.5.15) 学习笔记(十六)直接访问模式
直接访问模式(Direct mode)是ODAC最大的特色之一,即不需要安装Oracle客户端,ODAC越过了OCI(Oracle Call Interface ),使用TCP/IP协议就可以直接与O ...
- 重构第3天:方法提公(Pull Up Method)
理解:方法提公,或者说把方法提到基类中. 详解:如果大于一个继承类都要用到同一个方法,那么我们就可以把这个方法提出来放到基类中.这样不仅减少代码量,而且提高了代码的重用性. 看重构前的代码: usin ...
- 一起做RGB-D SLAM 第二季 (一)
小萝卜:师兄!过年啦!是不是很无聊啊!普通人的生活就是赚钱花钱,实在是很没意思啊! 师兄:是啊…… 小萝卜:他们都不懂搞科研和码代码的乐趣呀! 师兄:可不是嘛…… 小萝卜:所以今年过年,我们再做一个S ...
- CentOS下Redis服务器安装配置
说明: 操作系统:CentOS 1.安装编译工具 yum install wget make gcc gcc-c++ zlib-devel openssl openssl-devel pcre-de ...
- jQuery手机端上拉刷新下拉加载更多页面
基于jQuery手机端上拉下拉刷新页面代码.这是一款类似QQ空间客户端或者微信下拉刷新页面特效代码.效果图如下: 在线预览 源码下载 实现的代码. html代码: <div id=" ...
- 充实你的素材库!10款免费的 PSD 素材下载
由于网页设计师没有时间来自己从零开始设计,所以在设计项目中使用网络上已有的设计素材是常见的方式.这就是为什么我们经常会到网上搜索可以免费下载的素材. 今天,我们这里有几套不同的免费的 PSD 素材分享 ...
- LeetCode—— Median of Two Sorted Arrays
Description: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the medi ...
- 探秘重编译(Recompilations)(1/2)
这篇文章我想谈下SQL Server里一个非常重要的性能调优话题:重编译(Recompilations) .当你执行非常简单的存储过程(使用临时表)时,就会发生.今天我想奠定SQL Server里重编 ...
- 内存中OLTP(Hekaton)里的事务日志记录
在今天的文章里,我想详细讨论下内存中OLTP里的事务日志如何写入事务日志.我们都知道,对于你的内存优化表(Memory Optimized Tables),内存中OLTP提供你2个持久性(durabi ...