Android 控件:CheckBox
首先,在布局文件里注冊CheckBox
activity_main.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=".MainActivity"> <TextView android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> <CheckBox
android:id="@+id/eatID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="吃饭"
/>
<CheckBox
android:id="@+id/sleepID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="睡觉"/> <CheckBox
android:id="@+id/dotaID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="dota"/>
</LinearLayout>
MainActivity.java
CheckBox 即能够绑定 view.OnClickListener,也能够绑定 CompoundButton.OnCheckedChangeListener.
package com.example.litingdong.checkboxtest; import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton; public class MainActivity extends ActionBarActivity { private CheckBox eatBox;
private CheckBox sleepBox;
private CheckBox dotaBox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
eatBox=(CheckBox)findViewById(R.id.eatID);
sleepBox=(CheckBox)findViewById(R.id.sleepID);
dotaBox=(CheckBox)findViewById(R.id.dotaID);
//OnClickListener
OnButtonClickListener listener=new OnButtonClickListener();
listener = new OnButtonClickListener();
eatBox.setOnClickListener(listener);
sleepBox.setOnClickListener(listener);
dotaBox.setOnClickListener(listener); //OnCheckedChangedListener
CheckBoxListener changeListener =new CheckBoxListener();
eatBox.setOnCheckedChangeListener(changeListener);
sleepBox.setOnCheckedChangeListener(changeListener);
dotaBox.setOnCheckedChangeListener(changeListener);
} class OnButtonClickListener implements View.OnClickListener{
@Override
public void onClick(View v) {
CheckBox box=(CheckBox)v; if (box.getId()==R.id.eatID) {
System.out.println("eatBox");
}else if(box.getId()==R.id.sleepID){
System.out.println("sleepBox"); }else{
System.out.println("dotaBox");
} if (box.isChecked()){
System.out.println("Checked!");
}else{
System.out.println("UnChecked!");
}
}
} class CheckBoxListener implements CompoundButton.OnCheckedChangeListener{ @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (buttonView.getId()==R.id.eatID){
System.out.println("eatBox");
}else if (buttonView.getId()==R.id.sleepID){
System.out.println("sleepBox");
}else{
System.out.println("dotaBox");
} if (isChecked){
System.out.println("Checked!");
}else{
System.out.println("UnChecked!");
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId(); //noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
} return super.onOptionsItemSelected(item);
}
}
logcat:
Android 控件:CheckBox的更多相关文章
- Android控件系列之CheckBox
学习目的: 1.掌握在Android中如何建立CheckBox 2.掌握CheckBox的常用属性 3.掌握CheckBox选中状态变换的事件(监听器) CheckBox简介: CheckBox和Bu ...
- 一步一步学android控件(之十六)—— CheckBox
根据使用场景不同,有时候使用系统默认的CheckBox样式就可以了,但是有时候就需要自定义CheckBox的样式.今天主要学习如何自定义CheckBox样式.在CheckBox状态改变时有时需要做一些 ...
- Android控件系列之RadioButton&RadioGroup(转)
学习目的: 1.掌握在Android中如何建立RadioGroup和RadioButton 2.掌握RadioGroup的常用属性 3.理解RadioButton和CheckBox的区别 4.掌握Ra ...
- 一步一步学android控件(之十五) —— DegitalClock & AnalogClock
原本计划DigitalClock和AnalogClock单独各一篇来写,但是想想,两个控件的作用都一样,就和在一起写一篇了. DegitalClock和AnalogClock控件主要用于显示当前时间信 ...
- Android控件TextView的实现原理分析
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/8636153 在前面一个系列的文章中,我们以窗口 ...
- Android控件介绍
1. 介绍 Android控件大多位于android.widget, android.view.View为他们的父类对于Dialog系列, android.app.Dialog为父类 Android的 ...
- [Android Pro] android控件ListView顶部或者底部也显示分割线
reference to : http://blog.csdn.net/lovexieyuan520/article/details/50846569 在默认的Android控件ListView在 ...
- Android控件Gridview实现仿支付宝首页,Fragment底部按钮切换和登录圆形头像
此案例主要讲的是Android控件Gridview(九宫格)完美实现仿支付宝首页,包含添加和删除功能:Fragment底部按钮切换的效果,包含四个模块,登录页面圆形头像等,一个小项目的初始布局. 效果 ...
- Android 控件架构及View、ViewGroup的测量
附录:示例代码地址 控件在Android开发的过程中是必不可少的,无论是我们在使用系统控件还是自定义的控件.下面我们将讲解一下Android的控件架构,以及如何实现自定义控件. 1.Android控件 ...
- Android - 控件android:ems属性
Android - 控件android:ems属性http://blog.csdn.net/caroline_wendy/article/details/41684255?utm_source=tui ...
随机推荐
- Ubuntu下安装git工具
环境:Ubuntu 9.10 git-1.8.2.3.tar.bz2 1.将安装包下载到所选文件夹下,如:/tmp 2.tar -xjf git-1.8.2.3.tar.bz2 3.cd git-1. ...
- 【v2.x OGE教程 17】事务处理
游戏代码中常常有些逻辑须要处理.因此OGE引擎加入了一个IUpdateHandler的类. IUpdateHandler类是OGE引擎中使用频率很之高的组件之中的一个,其本身是一个接口.内部有onUp ...
- Codeforces 11B Jumping Jack(数学)
B. Jumping Jack time limit per test 1 second memory limit per test 64 megabytes input standard input ...
- How to search Installed Updates
Windows本身的控制面板中自带的搜索,无法根据补丁编号进行搜索 可以将补丁信息导出到文本,再用文本编辑器进行查找 https://www.concurrency.com/blog/w/search ...
- Magento--修改已存在的订单的运费
遇到一种情况,需要在下单后再由管理员添加订单运费,然后顾客再付款.那么问题来了,如何给订单添加运费呢?下面是一段代码,可以实现该功能: $orderId = 'your order id';$orde ...
- TYVJ 1935 拆点网络流
思路: 就是一个多重匹配 把每个防御塔拆成 拆成第j次 发射的导弹 跑个网络流 //By SiriusRen #include <cmath> #include <queue> ...
- 使用java自带的xml解析器解析xml
使用java自带的xml解析器解析xml,其实我不推荐,可以用Dom解析,或其他的方式,因为Java自带的解析器使用不但麻烦,且还有bug出现. 它要求,针对不同的xml(结构不同),必须写对应的ha ...
- Windows上Python2与Python3同时安装、共存
一.选择 Python2 还是 Python3?当然是全都要 Python3 虽是未来,不过 Python2 的用户群体仍然膨大,网上有大量优良的项目和模块可供使用,遇到问题也基本可以找到解决方法,推 ...
- PHP获取一周后的时间戳
echo strtotime("now");//相当于将英文单词now直接等于现在的日期和时间,并把这个日期时间转化为unix时间戳.这个效果跟echo time();一样. ec ...
- SpringBoot 整合 Mybatis 和 Mysql (详细版)
结构如下 1.引入相关依赖 <!--mysql--><dependency> <groupId>mysql</groupId> <artifact ...