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. Fiddler 接口测试(Composer)的使用方法

    原文:Fiddler 接口测试(Composer)的使用方法 下载地址:https://www.telerik.com/download/fiddler 一.Composer简介 右侧Composer ...

  2. [terry笔记]11gR2_DataGuard搭建_拷贝数据文件

    11gR2搭建dataguard环境: 自己做的实验,后续按照rman模式搭建.主备切换.模式调整等实验会陆续发上来. primary: OS:oel 6.4 database:11.2.0.4.0 ...

  3. root用户删除文件提示:Operation not permitted

    root用户删除文件提示:Operation not permitted http://blog.csdn.net/evanbai/article/details/6187578

  4. POJ——T 3687 Labeling Balls

    http://poj.org/problem?id=3687 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14842   ...

  5. HDU 3108 Ant Trip

    Ant Trip Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  6. how to deal with &quot;no such file error or diretory&quot; error for a new programmer in QT creator

    when i try to develop a hello demo in QT creator with the code following : #include<QApplication& ...

  7. ubuntu 14.04 桌面版关闭图形界面

    ubuntu 14.04 桌面版关闭图形界面 问题: 怎样将ubuntu14.04设置为文本模式启动? 解决方式: 改动改GRUB 的配置文件(不建议直接改 grub.conf) $sudo vim ...

  8. 用DOM动态控制表格

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  9. [NOI2002] Robot 解题报告(数论+DP)

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1408 Description 3030年,Macsy正在火星部署一批机器人. 第1秒,他 ...

  10. Dictionaries and tuples

    Dictionaries have a method called items that returns a list of tuples, where each tuple is a key-val ...