<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
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/genderGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<RadioButton
android:id="@+id/man"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"
/>
<RadioButton
android:id="@+id/woman"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"
/> </RadioGroup> </LinearLayout>
 import android.app.Activity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast; public class MainActivity extends Activity {
private RadioGroup genderGroup;
private RadioButton man;
private RadioButton woman;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); genderGroup = (RadioGroup) findViewById(R.id.genderGroup);
man = (RadioButton) findViewById(R.id.man);
woman = (RadioButton) findViewById(R.id.woman); RadioGroupListener Grouplistener = new RadioGroupListener();
genderGroup.setOnCheckedChangeListener(Grouplistener);
RadioButtonListener Buttonlistener = new RadioButtonListener();
man.setOnCheckedChangeListener(Buttonlistener);
woman.setOnCheckedChangeListener(Buttonlistener);
} class RadioButtonListener implements android.widget.CompoundButton.OnCheckedChangeListener{
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
System.out.println(isChecked);
} }
class RadioGroupListener implements OnCheckedChangeListener{
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(man.getId()==checkedId){
System.out.println("man");//这里不是简单的输出,要有针对的处理,比如更改数据库里的内容
Toast.makeText(MainActivity.this, "man", Toast.LENGTH_SHORT).show();
}else if(woman.getId()==checkedId){
System.out.println("woman");
Toast.makeText(MainActivity.this, "woman", Toast.LENGTH_SHORT).show();
}
} }
}

RadioGroup和CheckBox

 import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.app.Activity; public class Activity07 extends Activity {
//对控件对象进行声明
private RadioGroup genderGroup = null;
private RadioButton femaleButton = null;
private RadioButton maleButton = null;
private CheckBox swimBox = null;
private CheckBox runBox = null;
private CheckBox readBox = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.radio); //通过控件的ID来得到代表控件的对象
genderGroup = (RadioGroup)findViewById(R.id.genderGroup);
femaleButton = (RadioButton)findViewById(R.id.femaleButton);
maleButton = (RadioButton)findViewById(R.id.maleButton);
swimBox = (CheckBox)findViewById(R.id.swim);
runBox = (CheckBox)findViewById(R.id.run);
readBox = (CheckBox)findViewById(R.id.read); //为RadioGroup设置监听器,需要注意的是,这里的监听器和Button控件的监听器有所不同
genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override
public void onCheckedChanged(RadioGroup group, int checkedId) {//传进来的是控件的id
if(femaleButton.getId() == checkedId){
System.out.println("famale");//这里不是简单的输出,要有针对的处理,比如更改数据库里的内容
Toast.makeText(Activity07.this,"famale", Toast.LENGTH_SHORT).show();//用于提示用户
}
else if(maleButton.getId() == checkedId){
System.out.println("male");
Toast.makeText(Activity07.this,"male", Toast.LENGTH_SHORT).show();//用于提示用户
} }
}); //为多选按钮添加监听器
swimBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {//传进来的是是否选中
if(isChecked){
System.out.println("swim is checked");
}else{
System.out.println("swim is unchecked");
} }
}); runBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
System.out.println("run is checked");
}else{
System.out.println("run is unchecked");
} }
}); readBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
System.out.println("read is checked");
}else{
System.out.println("read is unchecked");
} }
});
}
}
 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RadioGroup
android:id="@+id/genderGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<RadioButton
android:id="@+id/femaleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/female"
/>
<RadioButton
android:id="@+id/maleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/male"
/>
</RadioGroup> <CheckBox
android:id="@+id/swim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/swim"
/>
<CheckBox
android:id="@+id/run"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/run"
/>
<CheckBox
android:id="@+id/read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/read"
/>
</LinearLayout>

控件_RadioGroup&&RadioButton(单选按钮)和Toast的更多相关文章

  1. android学习日记03--常用控件checkbox/radiobutton

    常用控件3.checkbox 复选框,确定是否勾选,点击一下勾选,点击第二下取消,当有一系列备选项时适合用checkbox控件,方便用户提交数据. 贴上例子Activity的java代码 packag ...

  2. C# 服务端控件 asp:RadioButton 选择选中值

    1.服务端控件RadioButton <asp:RadioButton ID="rbNewUser" runat="server" GroupName=& ...

  3. android基本控件学习-----RadioButton&CheckBox

    RadioButton(单选框)和CheckBox(复选框)讲解: 一.基本用法和事件处理 (1)RadioButton单选框,就是只能选择其中的一个,我们在使用的时候需要将RadioButton放到 ...

  4. Android控件:RadioButton(单选button)

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/ ...

  5. flutter控件之RadioButton

    import 'package:flutter/material.dart'; class LearnRadioButton extends StatefulWidget{ @override Sta ...

  6. Android基础控件单选按钮RadioButton和Checkbox复选按钮的使用

    1.相关简介 RadioButton需要和RadioGroup结合使用,在RadioGroup设置布局方式! Checkbox是单独使用,本文为了方便放在了RadioGroup中! 2.简单使用 方法 ...

  7. Android_常用控件及适配器

    TextView 控件中显示的内容必须是文本 TextView中常用的属性 android:text TextView中显示的文本内容 android:textColor 字体颜色 格式为#RGB # ...

  8. Android 开源控件系列_1

    第一部分 个性化控件(View) 主要介绍那些不错个性化的View,包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView.Pro ...

  9. Windowsphone8外包团队——wp8控件学习资源整理

    一天一天学 windows phone 控件 之  Slider(十七) 摘要:Slider 是我们最常见的控件之一,看到最多的一般在两个地方 ,一个是声音的大小,一个是色域.控制声音大小的网上很多, ...

随机推荐

  1. Hibernate学习(二)———— 一级缓存和三种状态解析

    一.一级缓存和快照 什么是一级缓存呢? 很简单,每次hibernate跟数据库打交道时,都是通过session来对要操作的对象取得关联,然后在进行操作,那么具体的过程是什么样的呢? 1.首先sessi ...

  2. Hyperledger Fabric密码模块系列之BCCSP(四)

    前面说过要找时间介绍一下bccsp包下面的工厂factory,so here it is. 通过factory目前可以获得两类BCCSP实例,一个是上文说的sw,还有一个是通过pkcs11实现的. B ...

  3. Uboot 引导内核时加载地址与入口地址问题

    如果使用 mkimage 生成内核镜像文件的话,会在内核的前头加上了 64 bytes 的信息头,供建立 tag 之用.bootm 命令会首先判断 bootm xxx 这个指定的地址 xxx 与 -a ...

  4. MyBatis——MyEclipse中使用mybatis-generator

    mybatis-generator可以根据数据库的表来生成POJO类.mapper.xml和DAO接口,用这个插件会大大地提高开发的效率.网上虽然有一些使用这个插件的教程,但我单个试了并不能成功,会出 ...

  5. css中的float属性以及清除方法 (2011-09-03 17:36:26)

    CSS里面的浮动属性是布局的常用工具,只有真正了解它并熟练使用才能将它的优点发挥到极致. 许多页面中都有文字绕图效果,并且各区块分布得错落有置,很多朋友在自学CSS布局时为了做出这些效果往往会被div ...

  6. oracle 外连接以及用on和where 的区别

    Oracle  外连接(OUTER JOIN)包括以下: 左外连接(左边的表不加限制) 右外连接(右边的表不加限制) 全外连接(左右两表都不加限制) 对应SQL:LEFT/RIGHT/FULL OUT ...

  7. T-SQL:谓词和运算符(六)

    谓词一般有 where和having,check  谓词只计算 TRUE ,FALSE或者UNKNOWN  逻辑表达式  如 AND 和OR 1.IN 谓词的用法 SELECT orderid, em ...

  8. T-SQL基础(一)之简单查询

    名词解释 SQL: Structured Query Language,结构化查询语言,是一种在关系型数据库中用于管理数据的标准语言.SQL是一种声明式编程语言,即只需表明需要什么而无需关注实现细节( ...

  9. Perl爬虫的简单实现

    由于工作中有个项目需要爬取第三方网站的内容,所以在Linux下使用Perl写了个简单的爬虫. 相关工具 1. HttpWatch/浏览器开发人员工具 一般情况下这个工具是用不到的,但是如果你发现要爬取 ...

  10. js array 数组添加与删除数据

    //新建数组 var ids =new Array(); //向数组中添加数据 ids.push(1); //删除下标为i的数组元素 ids.remove(i); //自定义删除函数: Array.p ...