<?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
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="单选按钮" /> <RadioGroup
android:id="@+id/sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text= "男"
></RadioButton>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text= "女"
></RadioButton>
</RadioGroup>
<Button
android:id="@+id/select"
android:text="选择"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:topLeftRadius="5dip"
android:topRightRadius="5dip"
android:bottomLeftRadius="5dip"
android:bottomRightRadius="5dip"
></Button> </LinearLayout>

MainActivity.java

package cn.szy.com;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast; public class MainActivity extends Activity {
/** Called when the activity is first created. */
private RadioGroup group;
private Button button;
private TextView textView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
group = (RadioGroup) this.findViewById(R.id.sex);
button =(Button) this.findViewById(R.id.select);
textView = (TextView) this.findViewById(R.id.text);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
int len = group.getChildCount();
String msg = "您选择了";
textView.setText(msg); // for(int i=0;i<len;i++){
// RadioButton radiobutton = (RadioButton) group.getChildAt(i);
// if(radiobutton.isChecked()){
// msg = radiobutton.getText().toString();
// break;
// }
// }
RadioButton radiobutton1 = (RadioButton) group.getChildAt(0);
RadioButton radiobutton2 = (RadioButton) group.getChildAt(1);
if(radiobutton1.isChecked()){
Toast.makeText(MainActivity.this, "男", 1).show();
}else if(radiobutton2.isChecked()){
Toast.makeText(MainActivity.this, "女", 1).show();
} }
}); }
}

Java-Android 之单选按钮的运用的更多相关文章

  1. Java/Android引用类型及其使用分析

    Java/Android中有四种引用类型,分别是: Strong reference     - 强引用Soft Reference        - 软引用Weak Reference      - ...

  2. Atitti.java android反编译解决方案-----虚拟机方案

    Atitti.java android反编译解决方案-----虚拟机方案 哈哈,终极解决方案是虚拟机...c++也可以反编译为汇编代码,但无需担心,因为读懂汇编太麻烦..只要不能拿到c++源码就可.. ...

  3. Java Android HTTP实现总结

    Java Android HTTP实现总结 Http(Hypertext Transfer Protocol)超文本传输协议,是一个基于请求/响应模式的无状态的协议,Http1.1版给出了持续连接的机 ...

  4. Java Android 注解(Annotation) 及几个常用开源项目注解原理简析

    不少开源库(ButterKnife.Retrofit.ActiveAndroid等等)都用到了注解的方式来简化代码提高开发效率. 本文简单介绍下 Annotation 示例.概念及作用.分类.自定义. ...

  5. 性能优化之Java(Android)代码优化

    最新最准确内容建议直接访问原文:性能优化之Java(Android)代码优化 本文为Android性能优化的第三篇——Java(Android)代码优化.主要介绍Java代码中性能优化方式及网络优化, ...

  6. [Java][Android][Process] 暴力的服务能够解决一切,暴力的方式运行命令行语句

    不管是在Java或者Android中运行命令行语句殊途同归都是创建一个子进程运行调用可运行文件运行命令.类似于Windows中的CMD一样. 此时你有两种方式运行:ProcessBuilder与Run ...

  7. Xml解析之——Java/Android/Python

    Xml解析之——Java/Android/Python 一.Xml文件 test.xml <note> <to>George</to> <from>Jo ...

  8. Java&Android反编工具打包

    Java&Android反编工具: 1.Eclipse反编插件:安装到Eclipse后,可以简要的查看jar包中的*.class; 2.DoAPK:反编*.apk为smali和一些资源文件,可 ...

  9. React Native之(支持iOS与Android)自定义单选按钮(RadioGroup,RadioButton)

    React Native之(支持iOS与Android)自定义单选按钮(RadioGroup,RadioButton) 一,需求与简单介绍 在开发项目时发现RN没有给提供RadioButton和Rad ...

  10. java android 环境变量配置备忘录

    java android 环境变量配置备忘录,全新重装系统要用到,备忘下来; JAVA_HOME D:\Java\jdk1.6.0_07 CLASSPATH  .;%JAVA_HOME%\lib\dt ...

随机推荐

  1. BZOJ 3563 DZY Loves Chinese

    Description 神校XJ之学霸兮,Dzy皇考曰JC. 摄提贞于孟陬兮,惟庚寅Dzy以降. 纷Dzy既有此内美兮,又重之以修能. 遂降临于OI界,欲以神力而凌♂辱众生. 今Dzy有一魞歄图,其上 ...

  2. Java日志性能

    在任何系统中,日志都是非常重要的组成部分,它是反映系统运行情况的重要依据,也是排查问题时的必要线索.绝大多数人都认可日志的重要性,但是又有多少人仔细想过该怎么打日志,日志对性能的影响究竟有多大呢?今天 ...

  3. Choose the best route

    hdu 2680:http://acm.hdu.edu.cn/showproblem.php?pid=2680 这道题值得一提的两点:在图论中注意重边问题是必须的,有向无向也是同等重要的,如这道题 f ...

  4. [FJSC2014]折线统计

    [题目描述] 二维平面上有n 个点(xi, yi),现在这些点中取若干点构成一个集合S,对它们按照x 坐标排序,顺次连接,将会构成一些连续上升.下降的折线,设其数量为f(S).如下图中,1->2 ...

  5. 支付宝APP支付(Java后台生成签名具体步骤)

    /** *支付宝支付 * @param orderId 订单编号 * @param actualPay 实际支付金额 * @return */ private String getOrderInfoB ...

  6. linux远程管理工具

    一.常见的远程管理控制方式主要有以下几种 ①RDP(remote desktop protocol)协议 远程桌面协议,我们常用的windows操作系统就是的远程桌面管理就是基于该协议的. ②teln ...

  7. SDH,WDM, OTN, MSTP,Ethernet, PTN, IP RAN

    概要:对带宽的需求,加上IP化严重,光通信技术不断地进化.最早的技术就是SONET.SDH,后来的技术都是在此技术上不断地改进和发展,以太网技术是一种局域网技术. SDH带宽小,提高带宽出现了WDM波 ...

  8. 2014 ACM-ICPC Asia Anshan Regional Contest(Online Version)

    题目I - Osu! - HDU 5078 题目分析:最水的一道题吧,求两点间的距离和时间差值的最大比值 #include<stdio.h> #include<math.h> ...

  9. Intellij IDEA 导入Eclipse或MyEclipse的Web项目(旧版 转载)

    Intellij IDEA 导入Eclipse或MyEclipse的Web项目 博客分类: Intellig IDEA Intellij IDEAEclipseWeb  Intellij IDEA 导 ...

  10. VirtualBox的usb支持

    解决usb支持: 0. 下载Oracle_VM_VirtualBox_Extension_Pack-4.0.4-70112.vbox-extpack后双击即可采用VB安装,若还是用ark打开可人为设置 ...