<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. 大数据技术之_08_Hive学习_04_压缩和存储(Hive高级)+ 企业级调优(Hive优化)

    第8章 压缩和存储(Hive高级)8.1 Hadoop源码编译支持Snappy压缩8.1.1 资源准备8.1.2 jar包安装8.1.3 编译源码8.2 Hadoop压缩配置8.2.1 MR支持的压缩 ...

  2. JavaScript 深入之从原型到原型链

    1 .构造函数创建对象 我们先使用构造函数创建一个对象: function Person(){ } var p = new Person(); p.name = 'ccy'; console.log( ...

  3. Docker版本变化和新版安装

    Docker从1.13版本之后采用时间线的方式作为版本号,分为社区版CE和企业版EE. 社区版是免费提供给个人开发者和小型团体使用的,企业版会提供额外的收费服务,比如经过官方测试认证过的基础设施.容器 ...

  4. java命令运行带包的类

    类文件d:\test2.java package b; public class test2 {    public static void main(String[] args) {         ...

  5. 菜鸟入门【ASP.NET Core】9:RoutingMiddleware介绍以及MVC引入

    前言 前面介绍了使用app.Map来配置路由,但是对于一般不是特别大的项目来说,不使用Map来进行路由配置. 配置路由 我们首先需要在Startup.cs文件中的ConfigureServices方法 ...

  6. MVC 获取控制器名称和Action名称(转载)

    MVC在filter中如何获取控制器名称和Action名称   使用ActionExecutingContext对象可以获取控制器名称.Action名称.参数名称以及参数值.路由和Action返回值不 ...

  7. What are the differences between struct and class in C++?

    Question: This question was already asked in the context of C#/.Net. Now I'd like to learn the diffe ...

  8. LSApplicationQueriesSchemes--关于info.plist 第三方登录 添加URL Schemes白名单

    近期苹果公司iOS 9系统策略更新,限制了http协议的访问,此外应用需要在“Info.plist”中将要使用的URL Schemes列为白名单,才可正常检查其他应用是否安装. 当你的应用在iOS 9 ...

  9. js实现的玫瑰花

    <html> <head> <meta charset="utf-8"> <title>开心快乐每一天</title> ...

  10. Salesforce的翻译工作台

    翻译工作台 Salesforce提供了翻译工作台.在这里管理员可以对各种数据进行翻译设置,包括对象信息.字段信息.验证规则.错误信息等. 翻译工作台集中了翻译的内容,从而使得管理员或开发者不需要在其他 ...