<?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. office web apps

    http://technet.microsoft.com/zh-cn/library/ff431687.aspx http://www.cnblogs.com/erucy/archive/2012/0 ...

  2. pfSense软路由防火墙

    才刚刚架起来,等着入门呀.. 想作网络的QOS限流这些的...

  3. 【POI2003/2004 stage I】

    [原题在此] Let us consider a game on a rectangular board m x 1 consisting of m elementary squares number ...

  4. 修改窗口属性(全部都是SetWindowLong设置)

    说明: 以下函数对于POPUP窗口有效,对于子窗口好像不行. //最小化按钮有效 ::SetWindowLong(m_hWnd,GWL_STYLE,GetWindowLong(m_hWnd,GWL_S ...

  5. Features of Spring Web MVC

    21.1.1 Features of Spring Web MVC Spring Web Flow Spring Web Flow (SWF) aims to be the best solution ...

  6. 【HDOJ】1881 毕业bg

    01背包. #include <cstdio> #include <cstring> #include <cstdlib> #define MAXN 1005 ty ...

  7. cheerio返回数据格式

    通读cheerio API { options: { decodeEntities: false, withDomLvl1: true, normalizeWhitespace: false, xml ...

  8. 直接拿来用!最火的android开源项目(一)

    不好意思尊重一下作者咯.详情见:csdn:http://www.csdn.net/article/2013-05-03/2815127-Android-open-source-projects

  9. 登录MD5加盐处理

    一:解决方案资源管理器截图: 二:operatorDAL.cs代码 using System; using System.Collections.Generic; using System.Linq; ...

  10. POJ 2260(ZOJ 1949) Error Correction 一个水题

    Description A boolean matrix has the parity property when each row and each column has an even sum, ...