大家好,我们今天这一节要介绍的是RadioGroup 的组事件.RadioGroup 可将各自不同的RadioButton ,设限于同一个Radio 按钮组,同一个RadioGroup 组里的按钮,只能做出单一选择(单选题).

首先,我们先设计一个TextView Widget ,以及一个RadioGroup ,并将该RadioGroup 内放置两个RadioButton ,默认为都不选择,在程序运行阶段,利用onCheckedChanged 作为启动事件装置,让User选择其中一个按钮,显示被选择的内容,最的将RadioButton 的选项文字显示于TextView 当中.

下面我们看一下效果图:

  

strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="hello">Hello World, RadioGroupDemoActivity!</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:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

<!--第一個TextView -->
  <TextView
    android:id="@+id/myTextView"
    android:layout_width="228px"
    android:layout_height="49px"
    android:text="@string/str_radio_question1"
    android:textSize="30sp"   
  />   
<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:checked="true"
      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>

RadioGroupDemoActivity.java

package com.lp.ecjtu;

import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class RadioGroupDemoActivity extends Activity {
    /** Called when the activity is first created. */
    private TextView txt;
    private RadioGroup radiogroup;
    private RadioButton rbtn1,rbtn2;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        txt = (TextView) findViewById(R.id.myTextView);
        radiogroup = (RadioGroup) findViewById(R.id.myRadioGroup);
        rbtn1 = (RadioButton) findViewById(R.id.myRadioButton1);
        rbtn2 = (RadioButton) findViewById(R.id.myRadioButton2);
        radiogroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // TODO Auto-generated method stub
                //当选择的是第一个单选按钮的时候,把第一个单选按钮的内容传到TextView里面进行显示
                if(checkedId == rbtn1.getId()){
                    txt.setText(rbtn1.getText());
                }else if(checkedId == rbtn2.getId()){
                    txt.setText(rbtn2.getText());
                }
            }
        });
    }
}

单选项框RadioGroup的综合应用的更多相关文章

  1. vue表单选项框

    选项框选的内容在下面显示 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...

  2. 微信小程序——单选项

    对于小程序单选,官方文档已经贴出了代码,我这里也不做过多解释,只是分享给大家一个小功能 一般在单选或者多选时,都会出现“其他”这个选项,如果通过input焦点事件.失焦事件来控制,代码会很繁琐 这里可 ...

  3. [js开源组件开发]模拟下拉选项框select

    模拟下拉选项框select 在css3流行的情况下,下拉框还是无法满足PD的需求,所以有了autosearch,有了模拟下拉框.效果如下图: select DEMO请案例点击这里查看.http://w ...

  4. MIUI选项框开关样式模拟

    有IOS的开关模拟,当然也有MIUI的开关模拟 看到设置选项里面的开关样式,突发奇想地来试试    最终效果如图: 实现过程 1. 选项框checkbox 模拟开关当然需要一个选项框,这里用到了复选框 ...

  5. Android弹出选项框及指示箭头动画选择

     Android弹出选项框及指示箭头动画选择 Android原生的Spinner提供了下拉列表选项框,但在一些流行的APP中,原生的Spinner似乎不太受待见,而通常会有下图所示的下拉列表选项框 ...

  6. iOS webview加载html自定义选项框选词

    项目要求:webview加载html网址,内容为英文文本,需要获取文本上的单词 这个是最终效果图: 思路是先实现自定义的选项框(不带系统选项)再获取到滑选的单词: 实现的步骤: 首先是替换掉系统长按出 ...

  7. 微信小程序之自定义select下拉选项框组件

    知识点:组件,animation,获取当前点击元素的索引与内容 微信小程序中没有select下拉选项框,所以只有自定义.自定义的话,可以选择模板的方式,也可以选择组件的方式来创建. 这次我选择了组件, ...

  8. python+selenium七:下拉框、选项框、select用法

    # from selenium import webdriverfrom selenium.webdriver.common.action_chains import ActionChainsimpo ...

  9. js进阶 9-11 select选项框如何动态添加和删除元素

    js进阶 9-11 select选项框如何动态添加和删除元素 一.总结 一句话总结: 二.js进阶 9-11 select选项框如何动态添加和删除元素 1.案例说明 2.相关知识 Select 下拉列 ...

随机推荐

  1. hdu 1509 Windows Message Queue

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1509 Windows Message Queue Description Message queue ...

  2. jquery 源码学习(*)

    最近在做日志统计程序,发现对方的程序是在Jquery基础上进行开发的,而公司的网站的框架是prototype.而且我也早就想了解一下Jquery源码,故决定研究Jquery源码,模拟它的方法   Jq ...

  3. MEF(Managed Extensibility Framework) 微软平台插件化开发

    体验Managed Extensibility Framework精妙的设计   MEF(Managed Extensibility Framework)是.NET Framework 4.0一个重要 ...

  4. Linux I/O总结

    文件流 标准I/O文件流可用于单字节或多字节字符集.流的定向决定了所读写的是单字节还是多字节.流在最初创建时,并没有定向,此时如果在为定向的流上使用多字节I/O函数,那么该流被设置为宽定向的:如果在为 ...

  5. [转]network-manager与interfaces冲突

      [转]network-manager与interfaces冲突   http://blog.sina.com.cn/s/blog_48a45b9501010681.html   网络配置的两种方式 ...

  6. ASP.NET Web API 2 对 CORS 的支持

    CORS概念 跨域资源共享 (CORS) 是一种万维网联合会 (W3C) 规范(通常被认为是 HTML5 的一部分),它可让 JavaScript 克服由浏览器施加的同域策略安全限制. 所谓同域策略, ...

  7. Json.Net使用JSON Schema验证JSON格式

    Json.NET supports the JSON Schema standard via the JsonSchema and JsonValidatingReader classes. It s ...

  8. UIToolbar swift

    // // ViewController.swift // UILabelTest // // Created by mac on 15/6/23. // Copyright (c) 2015年 fa ...

  9. jta.properties transactions.properties Log already in use 解决方法

    当在resin里跑多个含有atomikos控制事物的项目时,会报错,Log already in use. 解决方法: 加jta.properties或者transactions.properties ...

  10. Notes of the scrum meeting(12.7)

    meeting time:18:30~19:10p.m.,December 7th,2013 meeting place:3号公寓一层 attendees: 顾育豪                   ...