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. linux 内核驱动--Platform Device和Platform_driver注册过程

    linux 内核驱动--Platform Device和Platform_driver注册过程 从 Linux 2.6 起引入了一套新的驱动管理和注册机制 :Platform_device 和 Pla ...

  2. 为什么你的 phpinfo() 无法显示

    一.问题描述 编写了一个php文件test.php,代码如下: <?php echo phpinfo(); ?> 浏览器访问了一下,却返回了 NULL. 二.问题定位及解决 网上查了下,大 ...

  3. $destroy

    ng-view 路由切换会触发 $destroy

  4. Bootstrap插件的使用

    昨天,我偶然间发现了它——BootStrap插件,它是一一套功能强大的前端组件.说起来,我跟这插件还真算得上有缘,我本来并不是去找这个插件的,我本来是找BootStarp Paginator这个分页插 ...

  5. HTML5,CSS3 与 Javascript 制作视频播放器

    早一段时间,有一直研究 HTML5 和 CSS3 ,自己也做了不少 Demo ,只是 HTML5 Video 和 Audio 由于自己平时比较喜欢留意不同的播放器,因此特别想做一个自己喜欢的设计,考虑 ...

  6. 动态修改log4net组件的日志文件名

    最近项目使用到log4net来记录日志,当然二话不说先到cnblogs上查看一下各位高手关于log4net的教程和心得主要参看了摩诘 的Log4Net使用指南 (确实是非常好的log4net的入门指南 ...

  7. C#调用C++编写的DLL函数, 以及各种类型的参数传递 z

    1. 如果函数只有传入参数,比如: C/C++ Code Copy Code To Clipboard //C++中的输出函数 int__declspec(dllexport) test(consti ...

  8. Android之parseSDKContent failed

    由于之前安装ADT之后就一直报parseSDKContent failed的错误,具体的信息为:java.lang.NullPointerException. 此问题的情况为在Eclipse下,And ...

  9. Unity3d 粒子工具注释

    今天群里有人问到瀑布粒子如何调整尺寸. 我查到ellipsoid可以调节产生粒子的空间范围,然后还需要调节粒子的长度.他那个瀑布太长了.max energy和min energy这两个参数可以用来控制 ...

  10. classLoader (一)

    不说废话,上代码吧. 随便写一个类,他是由appclassLoader加载的 package classLoaderExample; class Bean { public void test() { ...