xml文件:

<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"
tools:context="com.example.day03.MainActivity" >
<!-- RadioGroup 为单选框分组 -->
<RadioGroup
android:id="@+id/group_sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/man"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked = "true"
android:text="男" />
<RadioButton
android:id="@+id/woman"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"/> </RadioGroup>
<Button
android:id="@+id/button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确定"
android:onClick="click"/>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="学习的课程:"
/>
<CheckBox
android:id="@+id/language"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="语文"/>
<CheckBox
android:id="@+id/math"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="数学"/>
<CheckBox
android:id="@+id/english"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="英语"/>
<Button
android:id="@+id/buttton02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="commit"
android:onClick="click1"/> </LinearLayout>

源代码:

package com.example.day03;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast; public class MainActivity extends Activity {
RadioGroup radioGroup;
CheckBox[] checks;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroup = (RadioGroup) findViewById(R.id.group_sex);
checks = new CheckBox[3];
//找出对应的控件
checks[0] = (CheckBox) findViewById(R.id.language);
checks[1] = (CheckBox) findViewById(R.id.math);
checks[2] = (CheckBox) findViewById(R.id.english); }
public void click(View v){
//找出RadioGroup选中的radioButton
//方法一:通过RadioGroup的getCheckedRAdioButtonId()方法找到被选中的id
int id = radioGroup.getCheckedRadioButtonId();
RadioButton radioButton = (RadioButton) findViewById(id);
Toast.makeText(MainActivity.this, radioButton.getText().toString(), Toast.LENGTH_SHORT).show();
//方法二:遍历RadioGroup下面所有的RadioButton,找出选中的项
for (int i = 0; i < radioGroup.getChildCount(); i++) {
RadioButton radioButton1 = (RadioButton) radioGroup.getChildAt(i);
if(radioButton1.isChecked()){
Toast.makeText(MainActivity.this, radioButton1.getText().toString(), Toast.LENGTH_SHORT).show();
} }
}
//点击该按钮时调用该方法
public void click1(View v){
String result = "";
//遍历checks数组找出选中项
for (CheckBox check : checks) {
if(check.isChecked()){
result += check.getText().toString();
}
}
Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show();
} }

Android_RadioButton,CheckBox的更多相关文章

  1. 单项选择RadioButton和多项选择CheckBox的使用

     在Android中,可以通过RadioButton和RadioGroup的组合来实现单项选择的效果.而多项选择则是通过CheckBox来实现的. 1.单项选择RadioButton 我们知道,一 ...

  2. WPF CheckBox 样式

    <Style x:Key="FocusVisual"> <Setter Property="Control.Template"> < ...

  3. 计算Div标签内Checkbox个数或已被disabled的个数

    先看下面的html: 计算div内的checkbox个数:$('#divmod input[type="checkbox"]').length 计算div内checkbox被dis ...

  4. 前端开发:css技巧,如何设置select、radio 、 checkbox 、file这些不可直接设置的样式 。

    前言: 都说程序员有三宝:人傻,钱多,死得早.博主身边的程序“猿”一大半应了这三宝,这从侧面说明了一个问题,只有理性是过不好日子的.朋友们应该把工作与生活分开,让生活变得感性,让工作变得理性,两者相提 ...

  5. Razor语法中绑定一个值给checkbox

    在ASP.NET MVC开发中,需要绑定一个值给checkbox标签,如下面写法,它们运行时是没有问题,照样能跑. 看看上面的语法,在绑定时,它却出现绿浪线.提不绑定的值is not a valid ...

  6. Checkbox 模板和样式

    <Style TargetType="{x:Type CheckBox}"> <Setter Property="FontFamily" Va ...

  7. RadioButton与CheckBox

    笔者长期从事于数据库的开发,算了,不提当年了,因为一直用的是小语种(PowerBuilder),还是来说说这两个最常见的控件吧! RadioButton(单选)和CheckBox(多选) 先来看看继承 ...

  8. Listview的Item中有CheckBox、Button等的焦点处理

    ListView的item布局中有CheckBox.Button等会获取焦点的控件会抢走焦点,造成ListView的item点击事件相应不了. 解决方法:控件设置 android:clickable= ...

  9. 实现CheckBox的三种选中状态(全选、半选、不选)在GridView中模拟树形的功能

    度娘了很多帖子,只说三种状态要用图片替换来做,但没找到有用的例子,被逼自己写了一个 三方控件肯定是很多的,如jstree,可以直接用 由于公司的UDS限制,不能上传图片,只能文字说明了. 就是要在gr ...

随机推荐

  1. Oracle建表插数据等等

    Oracle的表的管理: 表名和列的命名规则,详见 数据库命名规范 . 必须以字母开头 . 长度不能超过30个字符 . 不能使用Oracle的保留字 . 只能使用如下字符 column_name-Z, ...

  2. 转载:简化IT程序员工作生活的4个窍门

    如果可以简化你的生活——少做枯燥的任务,将时间真正地用于完成事情,你愿不愿意去尝试?下面就让我一起来学一下如何让程序员工作生活变得简单的小窍门.如果你敢于倾听自己的心声,你会发现自己一天中的大多数时间 ...

  3. 【C++】命令行Hangman #2015年12月15日 00:20:27

    增加了可以在构造Hangman对象时通过传入参数设定“最大猜测次数”的功能.少量修改.# 2015年12月15日 00:20:22 https://github.com/shalliestera/ha ...

  4. js模块,类,继承,命名空间,私有属性等相关概念梳理

    js确切的说是一种基于对象的语言,和纯面向对象的语言(比如as)稍微有点区别,js中没有类的概念.虽然有继承但是基于原型的继承.随着前段越来越受重视,jser们利用js的一些特性他们制造出了和纯面向对 ...

  5. VI编辑器的使用方法

    一.vi的工作模式vi有两种工作模式:  编辑模式:用来输入和编辑文件的模式,屏幕上会显示用户的键入,按键不是被解释为命令执行,而是作为文本写到用户的文件中.  指令模式:用来编辑.存盘和退出文件的模 ...

  6. as3+java+mysql(mybatis) 数据自动工具(三)

    介绍一下数据类配置,该数据类配置主要用于需要将数据库 mysql 数据转换成 java 对象,再转换为 as3 对象的数据类 配置文件为 xml 格式. <objects> <obj ...

  7. 【CSS3】Advanced1:Rounded Corners

    1.Border radius The border-radius property can be used to working clockwise from top-left set border ...

  8. HDU 4135 Co-prime

    思路:直接用求(b,1)范围内互质的数,(a-1,1)范围内互质的数.再求反 就是敲一下容斥模板 #include<cstdio> #include<cstring> #inc ...

  9. 使用Visual Studio 2013编写可维护的本地可视化(natvis)

    在Visual Studio 2012中,我们介绍了创建可视化使用原生类型的能力natvis文件. Visual Studio 2013中包含了一些改进,使其更容易编写可视化的类,在内部利用收集来存储 ...

  10. HW3.6

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...