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. git checkout

        git checkout <branch_name> <file> 检出具体分支上的 具体文件    git checkout  --merge <branch_ ...

  2. 实验一DOS报告

    实验一.DOS命令解释程序的编写实验 13物联网  李名贵  201306104123 一.        实验目的 (1)认识DOS: (2)掌握命令解释程序的原理: (3)掌握简单的DOS调用方法 ...

  3. [HDU 1963] Investment

    Investment Time Limit:10000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu   Descrip ...

  4. CSS+DIV之强化background属性

    1.背景颜色属性(background-color),设定背景颜色=html中bgcolor属性.我来写一个红色背景的body,(也可以定义某个特定区域) 示例写法:body {} 2.背景图片属性( ...

  5. NEsper z

    对实时信息分析和处理,常常需要客户应用程序的开发相应功能.一般地,这些功能需要提供以下的处理流程,分析获取的数据,筛选数据,提取出有用的信息,然后将其通过特定的形式展现出来.由于具体实时信息的高并发性 ...

  6. boost总结之variant

    boost的variant库类似于联合体,但是联合体中只能接受POD类型,但variant中并无此限制,它可以接受任意的类型.   boost::variant <int, std::strin ...

  7. 安装qc 出现error An error occurred while attempting to connect to the database.

    When trying to install mercury quality center starter edition 9.0 on Windows XP media center, I am g ...

  8. 【原】Spark Standalone如何通过start-all.sh启动集群

    1.start-all.sh脚本分析 图1 start-all.sh部分内容 我们可以从start-all.sh脚本源文件中看到它其实是start-master.sh和start-slaves.sh两 ...

  9. ftp两种传输方式区别

    转自:http://linux.chinaitlab.com/server/806269.html FTP可用多种格式传输文件,通常由系统决定,大多数系统(包括UNIX系统)只有两种模式:文本模 式和 ...

  10. 洛谷P1220 关路灯

    洛谷1220 关路灯 题目描述 某一村庄在一条路线上安装了n盏路灯,每盏灯的功率有大有小(即同一段时间内消耗的电量有多有少).老张就住在这条路中间某一路灯旁,他有一项工作就是每天早上天亮时一盏一盏地关 ...