Android学习笔记——CheckBox
该工程的功能实现在一个activity中显示一个单选框和一个多选框
以下代码是MainActivity.java文件中的代码
package com.example.checkbox; import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast; public class MainActivity extends Activity {
//对控件对象进行声明
private RadioGroup gendergroup = null;
private RadioButton femaleButton = null;
private RadioButton maleButton = null;
private CheckBox swimBox = null;
private CheckBox runBox = null;
private CheckBox readBox = null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//通过控件的ID来得到代表控件的对象
gendergroup = (RadioGroup)findViewById(R.id.genderGroup);
femaleButton = (RadioButton)findViewById(R.id.femaleButton);
maleButton = (RadioButton)findViewById(R.id.maleButton);
swimBox = (CheckBox)findViewById(R.id.swim);
runBox = (CheckBox)findViewById(R.id.run);
readBox = (CheckBox)findViewById(R.id.read);
//设置监听器
gendergroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if(femaleButton.getId() == checkedId){
System.out.println("female");
Toast.makeText(MainActivity.this, "female", Toast.LENGTH_SHORT).show();
}
else if(maleButton.getId() == checkedId)
{
System.out.println("male");
Toast.makeText(MainActivity.this, "male", Toast.LENGTH_SHORT).show();
}
}
});
//为多选按钮添加监听器
swimBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked)
{
System.out.println("swim is checked");
}
else
{
System.out.println("swim is unchecked");
}
}
});
runBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked)
{
System.out.println("run is checked");
}
else
{
System.out.println("run is unchecked");
}
}
});
readBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked)
{
System.out.println("read is checked");
}
else
{
System.out.println("read is unchecked");
}
}
});
}
}
以下代码是activity_main.xml文件中的代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" > <TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world" /> <RadioGroup
android:id="@+id/genderGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
> <RadioButton
android:id="@+id/femaleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/female"
/> <RadioButton
android:id="@+id/maleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/male"
/>
</RadioGroup> <CheckBox
android:id="@+id/swim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/swim"
/>
<CheckBox
android:id="@+id/run"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/run"
/>
<CheckBox
android:id="@+id/read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/read"
/> </LinearLayout>
以下代码是string.xml文件中的代码
<?xml version="1.0" encoding="utf-8"?>
<resources> <string name="app_name">CheckBox</string>
<string name="hello_world">Hello world!</string>
<string name="female">女</string>
<string name="male">男</string>
<string name="swim">游泳</string>
<string name="run">跑步</string>
<string name="read">读书</string>
</resources>
Android学习笔记——CheckBox的更多相关文章
- 【转】 Pro Android学习笔记(十九):用户界面和控制(7):ListView
目录(?)[-] 点击List的item触发 添加其他控件以及获取item数据 ListView控件以垂直布局方式显示子view.系统的android.app.ListActivity已经实现了一个只 ...
- Android 学习笔记之Volley(七)实现Json数据加载和解析...
学习内容: 1.使用Volley实现异步加载Json数据... Volley的第二大请求就是通过发送请求异步实现Json数据信息的加载,加载Json数据有两种方式,一种是通过获取Json对象,然后 ...
- Android学习笔记进阶之在图片上涂鸦(能清屏)
Android学习笔记进阶之在图片上涂鸦(能清屏) 2013-11-19 10:52 117人阅读 评论(0) 收藏 举报 HandWritingActivity.java package xiaos ...
- android学习笔记36——使用原始XML文件
XML文件 android中使用XML文件,需要开发者手动创建res/xml文件夹. 实例如下: book.xml==> <?xml version="1.0" enc ...
- Android学习笔记之JSON数据解析
转载:Android学习笔记44:JSON数据解析 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,为Web应用开发提供了一种 ...
- udacity android 学习笔记: lesson 4 part b
udacity android 学习笔记: lesson 4 part b 作者:干货店打杂的 /titer1 /Archimedes 出处:https://code.csdn.net/titer1 ...
- Android学习笔记36:使用SQLite方式存储数据
在Android中一共提供了5种数据存储方式,分别为: (1)Files:通过FileInputStream和FileOutputStream对文件进行操作.具体使用方法可以参阅博文<Andro ...
- Android学习笔记之Activity详解
1 理解Activity Activity就是一个包含应用程序界面的窗口,是Android四大组件之一.一个应用程序可以包含零个或多个Activity.一个Activity的生命周期是指从屏幕上显示那 ...
- Pro Android学习笔记 ActionBar(1):Home图标区
Pro Android学习笔记(四八):ActionBar(1):Home图标区 2013年03月10日 ⁄ 综合 ⁄ 共 3256字 ⁄ 字号 小 中 大 ⁄ 评论关闭 ActionBar在A ...
随机推荐
- Nginx之负载均衡服务器揭秘
Nginx代理服务器, 一次性代理多台后端机器, 利用负载算法, 决定将当前请求传递给某台服务器执行. 有哪些后台服务器?例如微软的IIS,Apache,Nginx 负载算法是什么? 加权轮询. ng ...
- Mysql存储引擎之TokuDB以及它的数据结构Fractal tree(分形树)
在目前的Mysql数据库中,使用最广泛的是innodb存储引擎.innodb确实是个很不错的存储引擎,就连高性能Mysql里都说了,如果不是有什么很特别的要求,innodb就是最好的选择.当然,这偏文 ...
- 关于二叉排序树 BST
#include<stdio.h> #include<stdlib.h> typedef struct node { double w; struct node *l,*r; ...
- KVC、KVO、NSNotification、delegate 总结及区别
1.KVC,即是指 NSKeyValueCoding,一个非正式的Protocol,提供一种机制来间接访问对象的属性.而不是通过调用Setter.Getter方法访问.KVO 就是基于 KVC 实现的 ...
- 欢迎加入.Net高级部落 173844862
本群成立至今3年多,聚集了好多大牛级别的人物,欢迎高手前来指教,也欢迎小菜鸟进来学习.群成员热情开朗,有问必答.在这里聊聊技术,谈谈理想,不光技术会得到提高,也会收获一大帮志同道合的朋友,希望在未来的 ...
- python 基础 基本数据类型
基本类型的补充 str --> 一次性创建的,不能被修改,强制修改就会在创建一个而之前的也会在 list -->允许重复的集合 修改 记录 链表,下一个元素的位置,上一个元素的位置 tu ...
- centos 开启VNC
安装软件包(有yum源安装的,不采用源码安装) yum -y install vnc vnc-server 安装成功后,配置如下: [root@localhost ~]# vncserver #设置 ...
- 【caffe】train_lenet.sh在windows下的解决方案
@tags: caffe python 在windows下配置caffe后,跑mnist手写数字识别的例子.发现train_lenet.sh不能运行. 那就写个python脚本替代吧. step1 定 ...
- 【BZOJ-3895】取石子 记忆化搜索 + 博弈
3895: 取石子 Time Limit: 1 Sec Memory Limit: 512 MBSubmit: 263 Solved: 127[Submit][Status][Discuss] D ...
- 【SDOI2010题集整合】BZOJ1922~1927&1941&1951&1952&1972&1974&1975
BZOJ1922大陆争霸 思路:带限制的单源最短路 限制每个点的条件有二,路程和最早能进入的时间,那么对两个值一起限制跑最短路,显然想要访问一个点最少满足max(dis,time) 那么每次把相连的点 ...