RadioGroup单选按钮用法,还是先看效果图

先中后,点RadioGroup测试按钮,可在标题栏显示选择结果,点清除可以清除选择。
下面上代码,main.xml:

<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:checkedButton="@+id/b1"
android:id="@+id/RG">
<!--默认选中b1-->
<RadioButton
android:text="1"
android:id="@+id/b1"
/>
<RadioButton
android:text="2"
android:id="@+id/b2"
/>
<RadioButton
android:text="3"
android:id="@+id/b3"
/>
</RadioGroup>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/show"
android:text="RadioGroup测试"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/clear"
android:text="清除"
/>

程序代码:

package com.pocketdigi;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup; public class main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("RadioGroup测试");
setContentView(R.layout.main);
RGDemo();
}
RadioGroup rg;
RadioButton b1;
RadioButton b2;
RadioButton b3;
public void RGDemo(){
rg=(RadioGroup)findViewById(R.id.RG);
b1=(RadioButton)findViewById(R.id.b1);
b2=(RadioButton)findViewById(R.id.b2);
b3=(RadioButton)findViewById(R.id.b3);
Button clr=(Button)findViewById(R.id.clear);
clr.setOnClickListener(clear);
Button echo=(Button)findViewById(R.id.show);
echo.setOnClickListener(show);
}
private Button.OnClickListener clear=new OnClickListener(){ @Override
public void onClick(View v) {
// TODO Auto-generated method stub
rg.clearCheck();
setTitle("RadioGroup测试");
} };
private OnClickListener show=new OnClickListener(){ @Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(b1.isChecked()){
setTitle("1");
}
if(b2.isChecked()){
setTitle("2");
}
if(b3.isChecked()){
setTitle("3");
}
} };
}

RadioGroup有一个onCheckChangeListener监听器,可以通过监听器的onCheckedChanged方法捕捉到点击事件,onCheckedChanged方法会传入一个int型的checkedId,可以通过对比传入的checkedId和RadioButton的ID,来确定被点中的选项.

rg.setOnCheckedChangeListener(new OnCheckedChangeListener(){

            @Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if(checkedId==b1.getId()){
Toast.makeText(main.this,"b1选中", Toast.LENGTH_LONG).show();
}
if(checkedId==b2.getId()){
Toast.makeText(main.this,"b2选中", Toast.LENGTH_LONG).show();
}
if(checkedId==b3.getId()){
Toast.makeText(main.this,"b3选中", Toast.LENGTH_LONG).show();
} } });

Android控件-单选按钮RadioButton的更多相关文章

  1. Android基础控件单选按钮RadioButton和Checkbox复选按钮的使用

    1.相关简介 RadioButton需要和RadioGroup结合使用,在RadioGroup设置布局方式! Checkbox是单独使用,本文为了方便放在了RadioGroup中! 2.简单使用 方法 ...

  2. Android控件:RadioButton(单选button)

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/ ...

  3. Android控件系列之RadioButton&RadioGroup(转)

    学习目的: 1.掌握在Android中如何建立RadioGroup和RadioButton 2.掌握RadioGroup的常用属性 3.理解RadioButton和CheckBox的区别 4.掌握Ra ...

  4. Android控件介绍

    1. 介绍 Android控件大多位于android.widget, android.view.View为他们的父类对于Dialog系列, android.app.Dialog为父类 Android的 ...

  5. 一步一步学android控件(之十六)—— CheckBox

    根据使用场景不同,有时候使用系统默认的CheckBox样式就可以了,但是有时候就需要自定义CheckBox的样式.今天主要学习如何自定义CheckBox样式.在CheckBox状态改变时有时需要做一些 ...

  6. [Android Pro] android控件ListView顶部或者底部也显示分割线

    reference to  :  http://blog.csdn.net/lovexieyuan520/article/details/50846569 在默认的Android控件ListView在 ...

  7. Android控件Gridview实现仿支付宝首页,Fragment底部按钮切换和登录圆形头像

    此案例主要讲的是Android控件Gridview(九宫格)完美实现仿支付宝首页,包含添加和删除功能:Fragment底部按钮切换的效果,包含四个模块,登录页面圆形头像等,一个小项目的初始布局. 效果 ...

  8. Android 控件架构及View、ViewGroup的测量

    附录:示例代码地址 控件在Android开发的过程中是必不可少的,无论是我们在使用系统控件还是自定义的控件.下面我们将讲解一下Android的控件架构,以及如何实现自定义控件. 1.Android控件 ...

  9. Android - 控件android:ems属性

    Android - 控件android:ems属性http://blog.csdn.net/caroline_wendy/article/details/41684255?utm_source=tui ...

随机推荐

  1. PHP设计模式(三)抽象工厂模式(Abstract Factory)

    一.什么是抽象工厂模式 抽象工厂模式的用意为:给客户端提供一个接口,可以创建多个产品族中的产品对象 ,而且使用抽象工厂模式还要满足以下条件: 系统中有多个产品族,而系统一次只可能消费其中一族产品. 同 ...

  2. 【图灵杯 A】谷神的赌博游戏

    [题目链接]:http://oj.acmclub.cn/problem.php?cid=1164&pid=0 [题意] [题解] 把每个数字都%3处理; 会发现最后1的个数为n+1 2和0的个 ...

  3. POJ——T 1469 COURSES

    http://poj.org/problem?id=1469 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24197   ...

  4. LeetCode 232: Implement Queue using Stacks

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  5. spring揭秘 读书笔记 二 BeanFactory的对象注冊与依赖绑定

    本文是王福强所著<<spring揭秘>>一书的读书笔记 我们前面就说过,Spring的IoC容器时一个IoC Service Provider,并且IoC Service Pr ...

  6. JavaScript中的Array对象方法调用

    方法concat for  循环与for in 循环 <html> <head> <script type="text/javascript"> ...

  7. CodeForces 550B Preparing Olympiad(DFS回溯+暴力枚举)

    [题目链接]:click here~~ [题目大意] 一组题目的数目(n<=15),每一个题目有对应的难度,问你选择一定的题目(大于r个且小于l个)且选择后的题目里最小难度与最大难度差不小于x, ...

  8. 数据结构之fhq-treap

    本文内容部分转自某大佬博客:https://blog.csdn.net/CABI_ZGX/article/details/79963427 例题:https://www.luogu.org/probl ...

  9. laravel中的数据迁移和数据填充

    laravel中的数据迁移和数据填充 标签(空格分隔): php 生成迁移文件两种方式: 1 新建一个表的迁移文件 php artisan make:migration create_students ...

  10. CUDA笔记13

    在新的环境上用CUTIL的时候,出现了问题.无法解析的外部符号 __imp_cutCheckCmdLineFlag 问题描述: kernel.cu.obj : error LNK2019: 无法解析的 ...