Android_RadioButton,CheckBox
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的更多相关文章
- 单项选择RadioButton和多项选择CheckBox的使用
在Android中,可以通过RadioButton和RadioGroup的组合来实现单项选择的效果.而多项选择则是通过CheckBox来实现的. 1.单项选择RadioButton 我们知道,一 ...
- WPF CheckBox 样式
<Style x:Key="FocusVisual"> <Setter Property="Control.Template"> < ...
- 计算Div标签内Checkbox个数或已被disabled的个数
先看下面的html: 计算div内的checkbox个数:$('#divmod input[type="checkbox"]').length 计算div内checkbox被dis ...
- 前端开发:css技巧,如何设置select、radio 、 checkbox 、file这些不可直接设置的样式 。
前言: 都说程序员有三宝:人傻,钱多,死得早.博主身边的程序“猿”一大半应了这三宝,这从侧面说明了一个问题,只有理性是过不好日子的.朋友们应该把工作与生活分开,让生活变得感性,让工作变得理性,两者相提 ...
- Razor语法中绑定一个值给checkbox
在ASP.NET MVC开发中,需要绑定一个值给checkbox标签,如下面写法,它们运行时是没有问题,照样能跑. 看看上面的语法,在绑定时,它却出现绿浪线.提不绑定的值is not a valid ...
- Checkbox 模板和样式
<Style TargetType="{x:Type CheckBox}"> <Setter Property="FontFamily" Va ...
- RadioButton与CheckBox
笔者长期从事于数据库的开发,算了,不提当年了,因为一直用的是小语种(PowerBuilder),还是来说说这两个最常见的控件吧! RadioButton(单选)和CheckBox(多选) 先来看看继承 ...
- Listview的Item中有CheckBox、Button等的焦点处理
ListView的item布局中有CheckBox.Button等会获取焦点的控件会抢走焦点,造成ListView的item点击事件相应不了. 解决方法:控件设置 android:clickable= ...
- 实现CheckBox的三种选中状态(全选、半选、不选)在GridView中模拟树形的功能
度娘了很多帖子,只说三种状态要用图片替换来做,但没找到有用的例子,被逼自己写了一个 三方控件肯定是很多的,如jstree,可以直接用 由于公司的UDS限制,不能上传图片,只能文字说明了. 就是要在gr ...
随机推荐
- hdu4499Cannon(搜索)
链接 这样的叫迭代吗..最近多做些搜索题了要 分行分列搜 判断满足条件 #include <iostream> #include<cstdio> #include<cst ...
- 点这里进入ABP系列文章总目录
基于DDD的现代ASP.NET开发框架--ABP系列之1.ABP总体介绍 ABP是“ASP.NET Boilerplate Project (ASP.NET样板项目)”的简称. ASP.NET Boi ...
- 转载--C++ STL
转自:http://wenku.baidu.com/view/15d18b4533687e21af45a9a4.html 1.C++ STL 之所以得到广泛的赞誉,也被很多人使用,不只是提供了像vec ...
- LightOJ 1370 Bi-shoe and Phi-shoe 欧拉函数+线段树
分析:对于每个数,找到欧拉函数值大于它的,且标号最小的,预处理欧拉函数,然后按值建线段树就可以了 #include <iostream> #include <stdio.h> ...
- mysql服务启动 但端口未监听
mysql 启动了,用 localhost 可以连接,但是用 127.0.0.1 不能连接.可能的原因是 1. mysql为了增强安全性而跳过了端口监听,查看方法: 用mysql> SHOW V ...
- 启动Selenium RC —— 我的第一个shell
打开终端 1. 新建一个sh文件 $ vim a.sh 2. 写入以下内容 #! /bin/bash cd Desktop/selenium/jar java -jar selenium-server ...
- GTK+系统中的对话框(GTK+dialogs)
GTK+系统中的对话框(GTK+dialogs) GTK+系统中的对话框(GTK+ dialogs) 在接下来的章节中我们将着重介绍GTK+系统中的对话框. 对话框窗口是众多GUI应用程序中不可或缺的 ...
- 支持度(support)和置信度(confidence)
支持度(Support)的公式是:Support(A->B)=P(A U B).支持度揭示了A与B同时出现的概率.如果A与B同时出现的概率小,说明A与B的关系不大:如果A与B同时出现的非常频 ...
- tyvj P1519 博彩游戏(AC自动机+DP滚动数组)
P1519 博彩游戏 背景 Bob最近迷上了一个博彩游戏…… 描述 这个游戏的规则是这样的:每花一块钱可以得到一个随机数R,花上N块钱就可以得到一个随机序列:有M个序列,如果某个序列是产生的随机序列的 ...
- IP头部校验(转)
一:原理 当发送IP包时,需要计算IP报头的校验和: 1.把校验和字段置为0: 2.对IP头部中的每16bit进行二进制求和: 3.如果和的高16bit不为0,则将和的高16bit和低16bit反复相 ...