1.简介

RadioButton为单选按钮,当一个按钮被选中后,点击其他按钮无法使上一个按钮变为未选中状态。RadioGroup是可以容纳多个RadioButton的容器,

通过使用RadioGroup,可以实现每次只有一个处于按钮被选中状态。

2.构建

我们可以从From Widgets中选择单选按钮,构建图二所示界面。

XML代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/RelativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

<TextView
          android:id="@+id/textView"
         android:layout_marginTop="10dp"
         android:layout_width="match_parent"
         android:layout_height="250dp"
         android:text="@string/tx"    >   
     </TextView>
     
    <RadioGroup
        android:id="@+id/rg"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        
    <RadioButton
        android:id="@+id/radioButton1"
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:text="@string/rb1"/>
    
    <RadioButton
        android:id="@+id/radioButton2"
        android:layout_width="fill_parent"
        android:layout_height="74dp"
        android:text="@string/rb2" />

</RadioGroup>
</RelativeLayout>

3.代码

public class Activity1 extends Activity {
    private RadioButton rbutton1;
    private RadioButton rbutton2;
    private RadioGroup rgroup;
    private TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act1);
        rbutton1 = (RadioButton) findViewById(R.id.radioButton1);
        rbutton2 = (RadioButton) findViewById(R.id.radioButton2);
        rgroup = (RadioGroup) findViewById(R.id.rg);
        tv = (TextView) findViewById(R.id.textView);
        //为RadioGroup设置监听器
        rgroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == rbutton1.getId()) {
                    DisplayToast("you have chosen " + rbutton1.getText());
                } else {
                    DisplayToast("you have chosen " + rbutton2.getText());
                }
            }
        });
    }
      //自定义位置Toast
    public void DisplayToast(String string) {
        //最后一项表示内容显示时间
        Toast mToast = Toast.makeText(getApplicationContext(), string, Toast.LENGTH_LONG);
        //内容在底部显示,前一项表示左右移动,后一项表示上下移动
        mToast.setGravity(Gravity.BOTTOM, 0, 200);
        mToast.show();
    }

4.效果

Android开发--RadioButton的应用的更多相关文章

  1. Android开发-之认识palette

    Android开发中,Google工程师已经给我们封装好了很多的按钮,使得我们在开发中非常的方便和便捷. 那么今天就来认识一下常用的按钮,那么在之前的课程中我已经详细讲过了Button按钮,那么这里就 ...

  2. Android开发2:事件处理及实现简单的对话框(Toast,AlertDialog,Snackbar,TextInputLayout的使用)

    前言 啦啦啦~又要和大家一起学习Android开发啦,博主心里好激动哒~ 在上篇博文中,我们通过线性布局和基础组件的使用,完成了一个简单的学生课外体育积分电子认证系统的界面,本篇博文,将和大家一起熟悉 ...

  3. Android开发1:基本UI界面设计——布局和组件

    前言 啦啦啦~本学期要开始学习Android开发啦~ 博主在开始学习前是完完全全的小白,只有在平时完成老师要求的实验的过程中一步一步学习~从此篇博文起,博主将开始发布Android开发有关的博文,希望 ...

  4. 【Android学习】《Android开发视频教程》第一季笔记

    视频地址: http://study.163.com/course/courseMain.htm?courseId=207001 课时5    Activity基础概念 1.Android开发技术结构 ...

  5. Android开发学习清单

    目录: 第1章 Android应用与开发环境1.1 Android的发展和历史1.1.1 Android的发展和简介1.1.2 Android平台架构及特性1.2 搭建Android开发环境1.2.1 ...

  6. Android开发权威指南(第2版)新书发布

    <Android 开发权威指南(第二版)>是畅销书<Android开发权威指南>的升级版,内容更新超过80%,是一本全面介绍Android应用开发的专著,拥有45 章精彩内容供 ...

  7. Android开发中的问题及相应解决(持续更新)

    最近博客写的少了,以后还得经常更新才行. ------------------------------------------------------------ 1.特定业务需求下try cath ...

  8. Android开发代码规范(转)

    Android开发代码规范 1.命名基本原则    在面向对象编程中,对于类,对象,方法,变量等方面的命名是非常有技巧的.比如,大小写的区分,使用不同字母开头等等.但究其本,追其源,在为一个资源其名称 ...

  9. Xamarin Android开发实战(上册)大学霸内部资料

    Xamarin Android开发实战(上册)大学霸内部资料   试读文档下载地址:http://pan.baidu.com/s/1jGEHhhO 密码:vcfm 介绍: 本教程是国内唯一的Xamar ...

随机推荐

  1. RDIFramework.NET ━ 9.15 个性化设置 ━ Web部分

    RDIFramework.NET ━ .NET快速信息化系统开发框架 9.15  个性化设置 -Web部分 个性化设置,主要针对用户的偏好对界面进行设置,主界面如下: 9.15.1界面皮肤设置 目前框 ...

  2. Linux上F上传文件到FTP服务器

    Linux上上传跟Windows上上传不一样,在Windows上测试没问题,但是放到Linux服务器上跑,上传的文件中文显示乱码.解决方案: FtpUtil.java红色标记处 package cn. ...

  3. IntelliJ IDEA Community Edition 14.1.4下使用 Apache-Subversion搭建代码管理环境

    当前我的idea 版本是14.1.4. 1,)SVN Server下载与安装(https://www.visualsvn.com/server/): 因为我开发机是x64的,所以我优先下载 x64的 ...

  4. 深入理解Android之Gradle

    深入理解Android之Gradle 格式更加精美的PDF版请到:http://vdisk.weibo.com/s/z68f8l0xTYrZt 下载 Gradle是当前非常"劲爆" ...

  5. html5,新增的元素,fieldset legend

    <form action="">    <fieldset>        <legend>用户注册</legend>       ...

  6. flask学习资源

    1.flask快速入门(视频) http://www.pythoner.cn/course/topic/flask-web-development/ 2.flask教程,开源中国 http://www ...

  7. transition、animation在macbook air上图片动画边缘抖动

    示例: BUG描述: 最近同事一项目中,产品提出在macbook air上,列表图片放大效果边缘出现抖动现象.在retina屏上没有此问题. 调试过程: 1.单独把结构分离.确定是否由其他元素引起. ...

  8. angularJS——自定义指令

    主要介绍指令定义的选项配置 //angular指令的定义,myDirective ,使用驼峰命名法 angular.module('myApp', []) .directive('myDirectiv ...

  9. PDO处理大批量数据的入库

    <?php //最东端 东经135度2分30秒 //最西端 东经73度40分 //最南端 北纬3度52分 //最北端 北纬53度33分 //转换为度的单位$dbName = 'txhl';//数 ...

  10. 如何清理photoshop cs6 被升级的烦人的adobe creative cloud组件

    安装photoshop cs6(虽然目前已经退出到cc 2015,不过因激活成熟度等,我还是偏向于使用cs6,够用!),默认安装adobe application manager. 不过如果不小心单独 ...