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. Medium上关于git的文章

    rebase和merge的正确使用时机 https://medium.com/@porteneuve/getting-solid-at-git-rebase-vs-merge-4fa1a48c53aa ...

  2. Samba ‘dcerpc_read_ncacn_packet_done’函数缓冲区溢出漏洞

    漏洞名称: Samba ‘dcerpc_read_ncacn_packet_done’函数缓冲区溢出漏洞 CNNVD编号: CNNVD-201312-169 发布时间: 2013-12-12 更新时间 ...

  3. BZOJ1895: Pku3580 supermemo

    1895: Pku3580 supermemo Time Limit: 15 Sec  Memory Limit: 64 MBSubmit: 77  Solved: 47[Submit][Status ...

  4. apache点NET环境

    1.首先 下载一个mod_aspdotnet-2.2.0.2006-setup-r2.msi 2.安装好后在apache的moudles目录会生成一个mod_aspdotnet.so文件 3.修改ht ...

  5. C#检验数据有效性验证类

    using System; using System.Text; using System.Text.RegularExpressions; namespace Dachie.Common { /// ...

  6. HashPasswordForStoringInConfigFile 已过时

    在.net 4.5版本下,使用System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile进行MD5加密时,会出 ...

  7. [Android]在Dagger 2中Activities和Subcomponents的多绑定(翻译)

    以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/6266442.html 在Dagger 2中Activities ...

  8. Apache mod_wsgi部署Django项目

    学习python web开发,Django部署备忘 1.下载mod_wsgi,下载路径如下:http://code.google.com/p/modwsgi/downloads/list挑选For 2 ...

  9. Storm系列(十三)架构分析之Worker-维护ZMQ连接

    Worker根据Topology的定义及分配到自身的任务情况,计算出发出的消息被那些Task接收,由于Worker上分配的任务可能被调整,因此Worker需要定时的更新这些连接信息. ZMQ连接信息更 ...

  10. 【原创】Java移位运算

    学习移位运算,首先得知道参与移位运算的类型的位数,那先来复习下Java基础类型的占位数吧. Java基础类型 Java基础类型总结一览表 类型 二进制位数 最大值 最小值 初始化值 表示形式 带符号 ...