android CheckBox使用和状态获得
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"> <!-- 定义CheckBox控件 ,代表篮球选项-->
<CheckBox
android:id="@+id/CbBasketball"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="篮球" /> <!-- 定义CheckBox控件 ,代表乒乓球选项-->
<CheckBox
android:id="@+id/CbPingpangball"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="乒乓球" /> <!-- 定义CheckBox控件 ,代表足球选项-->
<CheckBox
android:id="@+id/CbFootball"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="足球" /> <!-- 定义TextView控件,来显示选中结果 -->
<TextView
android:id="@+id/TvResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/str" /> </LinearLayout>
package com.example.yanlei.yl2; import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
//导入必备的包
import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener; public class MainActivity extends AppCompatActivity { private CheckBox CbBasketball; //定义篮球的复选框对象
private CheckBox CbPingpangball; //定义乒乓球的复选框对象
private CheckBox CbFootball; //定义足球的复选框对象
private TextView TvResult; //定义结果文本便签对象 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); //调用父类的onCreate方法 //通过setContentView方法设置当前页面的布局文件为activity_main
setContentView(R.layout.activity_main);
findView(); //获取页面中的控件
setListener(); //设置控件的监听器
} private void setListener() {
// TODO Auto-generated method stub
//设置所有CheckBox的状态改变监听器
CbBasketball.setOnCheckedChangeListener(myCheckChangelistener);
CbPingpangball.setOnCheckedChangeListener(myCheckChangelistener);
CbFootball.setOnCheckedChangeListener(myCheckChangelistener);
} OnCheckedChangeListener myCheckChangelistener = new OnCheckedChangeListener() { @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
//设置TextView的内容显示CheckBox的选择结果
setText();
}
}; private void findView() {
// TODO Auto-generated method stub
//通过findViewById得到对应的控件对象
CbBasketball = (CheckBox)findViewById(R.id.CbBasketball);
CbPingpangball = (CheckBox)findViewById(R.id.CbPingpangball);
CbFootball = (CheckBox)findViewById(R.id.CbFootball);
TvResult = (TextView)findViewById(R.id.TvResult);
} private void setText(){
String str;
TvResult.setText(""); //清空TextView的内容
//如果CbBasketball被选中,则加入TvResult内容显示
if (CbBasketball.isChecked()) {
str = TvResult.getText().toString()+CbBasketball.getText().toString()+",";
TvResult.setText(str);
}
//如果CbPingpangball被选中,则加入TvResult内容显示
if (CbPingpangball.isChecked()) {
str = TvResult.getText().toString()+CbPingpangball.getText().toString()+",";
TvResult.setText(str);
}
//如果CbFootball被选中,则加入TvResult内容显示
if (CbFootball.isChecked()) {
str = TvResult.getText().toString()+CbFootball.getText().toString();
TvResult.setText(str);
}
} }
android CheckBox使用和状态获得的更多相关文章
- android checkbox 未选中状态 已选中状态 替换成自己的图片
效果图: 未选中状态: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA ...
- xamarin android checkbox自定义样式
xamarin android checkbox自定义样式 在drawable文件在新建checkbox_bg.xml文件 <?xml version="1.0" encod ...
- android开发获取网络状态,wifi,wap,2g,3g.工具类(一)
android开发获取网络状态整理: package com.gzcivil.utils; import android.content.Context; import android.net.Con ...
- Android获取当前网络状态
Android获取当前网络状态 效果图 有网络 没有网络 源码 下载地址(Android Studio工程):http://download.csdn.net/detail/q4878802/9052 ...
- jQuery判断复选框checkbox的选中状态
通过jQuery设置复选框为选中状态 复选框 <input type="checkbox"/> 错误代码: $("input").attr(&quo ...
- 获得android手机的联网状态
获得android手机的联网状态 在Android平台上开发基于网络的应用,必然需要去判断当前的网络连接情况.下面的代码,作为例子,详细说明了对于当前网络情况的判断. 先看一个自己定义的应用类. ...
- android CheckBox控件的定义及事件监听
http://www.beijibear.com/index.php?aid=336 android CheckBox控件的定义及事件监听,本例实现CheckBox控件的定义及点击事件的监听并显示结果 ...
- jquery 获取 checkbox 的 checked 状态问题
这个郁闷了,今天写这个功能的时候发现了问题,上网找了好多资料对照,更加纠结... 事实证明一切,自己测试了N遍,发现网上的说法和自己以前的理解都是错的,不知道大家有没发现. 下面来看看网上大多资料的说 ...
- [置顶]
android ListView包含Checkbox滑动时状态改变
题外话: 在xamarin android的开发中基本上所有人都会遇到这个小小的坎,的确有点麻烦,当时我也折腾了好一半天,如果你能看到这篇博客,说明你和我当初也是一样的焦灼,如果你想解决掉这个小小的坎 ...
随机推荐
- CSS 文本下划线 text-decoration
定义和用法 text-decoration 属性规定添加到文本的修饰. 可能的值 值 描述 none 默认.定义标准的文本. underline 定义文本下的一条线. overline 定义文本上 ...
- service worker 消息推送
https://developers.google.com/web/fundamentals/codelabs/push-notifications/?hl=en 首先下载源码: git clone ...
- 对uboot中CFG_和CONFIG_的理解
CONFIG_用于选择CPU SOC 板子的类型,系统时钟,设备驱动driver驱动等 CFG_用于设置malloc缓冲池的大小,偏移地址部分的定义,uboot的提示符,uboot的加载地址,fl ...
- Lex与Yacc学习(八)之变量和有类型的标记(扩展计算器)
变量和有类型的标记 下一步扩展计算器来处理具有单个字母名字的变量,因为只有26个字母 (目前只关心小写字母),所以我们能在26个条目的数组(称它为vbltable)中存储变量. 为了使得计算器更加有用 ...
- cs229_part3
接下来就是最最最重要的一个有监督学习算法了. 支持向量机 问题背景 样本集表示: \[(x,y)\in D, x\in R^n, y\in \{-1,+1\}\] 回到之前的逻辑回归模型中: 逻辑回归 ...
- 织梦dedecms自定义表单设置必填项
1. 用php验证 在plus/diy.php的第 40行下加 //增加必填字段判断 if($required!=''){ if(preg_match('/,/', $required)) { $re ...
- 一款基于jQuery的QQ表情插件
我们在QQ聊天或者发表评论.微博时,会有一个允许加入表情的功能,点击表情按钮,会弹出一系列表情小图片,选中某个表情图片即可发表的丰富的含表情的内容.今天和大家分享一款基于jQuery的QQ表情插件,您 ...
- pytorch中的math operation: torch.bmm()
torch.bmm(batch1, batch2, out=None) → Tensor Performs a batch matrix-matrix product of matrices stor ...
- Terracotta
Terracotta 3.2.1简介 (一) 博客分类: 企业应用面临的问题 Java&Socket 开源组件的应用 hibernatejava集群服务器EhcacheQuartzTerrac ...
- 关于JS中字符串赋值的问题
JS中不能直接 字符串不能 str[i] = 'x' 不能for循环 字符串length 然后赋值 应该 将字符串转换为数组 而且 字符x[i]=* 不是所有浏览器都兼容的 用 spl ...