1.布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:orientation="horizontal"
android:layout_height="wrap_content" > <RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="男" /> <RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女" /> </RadioGroup> </LinearLayout>

2.绑定事件(实现接口,注意导包)

package com.example.test2;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener; public class MainActivity extends Activity implements OnCheckedChangeListener { private Button login_button;
private Button reset;
private RadioGroup rg;
private Button other;
private CheckBox checkBox1;
private Button myButton; /*
* @Override protected void onCreate(Bundle savedInstanceState) {
* super.onCreate(savedInstanceState);
* setContentView(R.layout.activity_radiobutton);
*
* login_button = (Button) this.findViewById(R.id.loginButton); checkBox1 =
* (CheckBox) findViewById(R.id.checkBox1);
* checkBox1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
*
* @Override public void onCheckedChanged(CompoundButton arg0, boolean arg1)
* { // arg1代表是否选中 Log.i("tag", arg1 + ""); if (arg1) { Log.i("tag",
* checkBox1.getText().toString()); }
*
* } }); }
*/ // 单选框
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_radiobutton); rg = (RadioGroup) this.findViewById(R.id.radioGroup1);
rg.setOnCheckedChangeListener(this);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} @Override
public void onCheckedChanged(RadioGroup arg0, int checkId) {
// TODO Auto-generated method stub
switch(checkId){
case R.id.radio0:
Log.i("tag", "当前选中男");
break ;
case R.id.radio1:
Log.i("tag", "当前选中女");
break ;
default:
break ;
}
} }

3.效果:

RadioGroup和RadioButton(单选框)的更多相关文章

  1. Tkinter 之RadioButton单选框标签

    一.参数说明 语法 作用 Radiobutton(root,text='xxxx') 单选框文本显示内容 Radiobutton(root,variable=color) 单选框索引变量,通过变量的值 ...

  2. tkiner中Radiobutton单选框控件(七)

    Radiobutton控件 由于本次内容中好多知识都是之前重复解释过的,本次就不做解释了.不太清楚的内容请参考tkinter1-6节中的内容 import tkinter wuya = tkinter ...

  3. tkinter中Radiobutton单选框控件(七)

    Radiobutton控件 由于本次内容中好多知识都是之前重复解释过的,本次就不做解释了.不太清楚的内容请参考tkinter1-6节中的内容 import tkinter wuya = tkinter ...

  4. Qt Quick 常用元素:RadioButton(单选框),CheckBox(复选框) 与 GroupBox(分组框)

    先介绍一下 ExclusiveGroup. ExclusiveGroup (互斥分组)本身是不可见元素,用于将若干个可选择元素组合在一起, 供用户选择其中的一个选项.你可以在 ExclusiveGro ...

  5. Android初级教程小案例之单选框RadioGroup与复选框CheckBox

    Android里面的单选框和html中的其实是一样的效果.这里用到两个控件:CheckBox和RadioGroup.直接上代码: radio.xml布局文件: <?xml version=&qu ...

  6. 可分组的选择框控件(MVVM下)(Toggle样式 仿造单选框RadioButton,复选框CheckBox功能)

    原地址: http://www.cnblogs.com/yk250/p/5660340.html 效果图如下:支持分组的单选框,复选框样式和MVVM下功能的实现.这是项目中一个快捷键功能的扩展. 1, ...

  7. WPF中单选框RadioButton

    单选框RadioButton的基本使用: <StackPanel Margin="10"> <Label FontWeight="Bold"& ...

  8. vue之v-for遍历下拉框select和单选框组radio-group

    1.v-for遍历下拉框 <el-form-item label="审核状态:" prop="status"> <el-select v-mo ...

  9. Android入门(八):使用RadioGroup 和RadioButton组件建立单选清单

    这一章,我们学习RadioGroup 和RadioButton组件,我们新建一个项目,编码过程与前几章的项目类似. 1.建立字符串资源文件strings.xml: <resources> ...

随机推荐

  1. git的使用入门

    写作目的: 快速的上手git版本控制+github神器进行基本的版本同步操作. 怎么做? 对于任意一个代码项目,使用git_bash进入到代码目录 如果没有进行过初始化操作:应当使用git init  ...

  2. 1911: [Apio2010]特别行动队(斜率优化)

    链接 思路 斜率优化dp. 代码 #include<cstdio> #include<algorithm> #include<cstring> #include&l ...

  3. viewpager 无网络的时候滑动异常

    不知道大家有没有遇到过这种情况,就是框架是viewpager+fragment的架构.然后呢,fragment里面是webview.一般情况下,当没有网的时候,webviwe会说什么找不到网页,然后很 ...

  4. CSS3 loading效果全

    梦想天空 关注前端开发技术 ◆ 推动 HTML5 & CSS3 技术发展 ◆ 本博客全新站点:yyyweb.com 欢迎围观:) 首页 管理 订阅 网页设计 JavaScript jQuery ...

  5. 剑指Offer - 九度1514 - 数值的整数次方

    剑指Offer - 九度1514 - 数值的整数次方2013-11-30 00:49 题目描述: 给定一个double类型的浮点数base和int类型的整数exponent.求base的exponen ...

  6. 《Cracking the Coding Interview》——第18章:难题——题目5

    2014-04-29 01:51 题目:你有一个文本文件,每行一个单词.给定两个单词,请找出这两个单词在文件中出现的其中一对位置,使得这两个位置的距离最短. 解法:我的思路是建立倒排索引,计算出所有单 ...

  7. 嵌入式(Embedded System)笔记 —— Cortex-M3 Introduction and Basics(上)

    随着课内的学习,我想把每节课所学记录下来,以作查阅.以饲读者.由于我所上的是英文班课程,因此我将把关键术语的英文给出,甚至有些内容直接使用英文. 本次所介绍内容是关于Cortex-M3的基础内容. - ...

  8. android中dumpsys命令使用

    转自:https://testerhome.com/topics/1462 adb shell dumpsys,默认打印出当前系统所有service信息,在后面可加上具体的服务名 需要列出当前运行的服 ...

  9. 孤荷凌寒自学python第四十四天Python操作 数据库之准备工作

     孤荷凌寒自学python第四十四天Python操作数据库之准备工作 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 今天非常激动地开始接触Python的数据库操作的学习了,数据库是系统化设计 ...

  10. Ubuntu系列问题

    一.Ubuntu16.04 intel_rapl : no valid rapl domains found in packge0 echo 'blacklist intel_rapl' >&g ...