1)ChexkBox继承自CompoundButton组件;

2)isChecked()--确定是否选中;setChecked(bool checked)--设置选中或取消选中;

3)监听事件:CompoundButton.OnCheckedChangeListener

使用checkbox,并实现监听测试:

1)效果:

2)源代码:

res\layout\activity_main.xml

<RelativeLayout 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"
tools:context=".MainActivity" > <CheckBox
android:id="@+id/cbJava"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="27dp"
android:text="Java Runtime 9.0" /> <TextView
android:id="@+id/tvCheckedValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/cbJava"
android:layout_below="@+id/cbPython"
android:layout_marginLeft="14dp"
android:layout_marginTop="54dp"
android:text="" /> <CheckBox
android:id="@+id/cbPython"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/cbJava"
android:layout_below="@+id/cbJava"
android:layout_marginTop="22dp"
android:text="Python runtime2.7" /> </RelativeLayout>

MainActivity.java

 package com.example.helloword;

 import java.text.BreakIterator;

 import android.R.bool;
import android.R.string;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.renderscript.Script.KernelID;
import android.test.IsolatedContext;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView; public class MainActivity extends Activity {
private CheckBox cbJava, cbPython;
private TextView tvCheckedValue; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.cbJava = (CheckBox) this.findViewById(R.id.cbJava);
this.cbPython = (CheckBox) this.findViewById(R.id.cbPython);
this.tvCheckedValue = (TextView) this.findViewById(R.id.tvCheckedValue); CheckBoxOnCheckedChangeListener listener = new CheckBoxOnCheckedChangeListener(); this.cbJava.setOnCheckedChangeListener(listener);
this.cbPython.setOnCheckedChangeListener(listener); } private StringBuffer stringBuffer; private class CheckBoxOnCheckedChangeListener implements
CompoundButton.OnCheckedChangeListener {
private String checkJava = cbJava.getText().toString() + " ";
private String checkPython = cbPython.getText().toString() + " ";
private StringBuffer stringBuffer = new StringBuffer(); @Override
public void onCheckedChanged(CompoundButton compoundButton,
boolean ischecked) {
switch (compoundButton.getId()) {
case R.id.cbJava:
Log.i("info", "operator " + cbJava.getText());
break;
case R.id.cbPython:
Log.i("info", "operator " + cbPython.getText());
break;
default:
break;
} if (compoundButton == cbJava) {
changeValue(ischecked, checkJava);
} else if (compoundButton == cbPython) {
changeValue(ischecked, checkPython);
} tvCheckedValue.setText(stringBuffer.toString());
} private void changeValue(boolean ischecked, String checkValue) {
int start = stringBuffer.indexOf(checkValue);
int end = start + checkValue.length();
if (ischecked) {
if (start == -1) {
stringBuffer.append(checkValue);
}
} else {
if (start != -1) {
stringBuffer.delete(start, end);
}
}
}
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} @Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
// 当点击回退时,弹出该窗口(也就相当于关闭操作)
if (keyCode == KeyEvent.KEYCODE_BACK) {
new AlertDialog.Builder(this).setTitle("是否退出?")
.setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
finish();
}
}).setNegativeButton("取消", null).show();
return true;
}
return super.onKeyUp(keyCode, event);
}
}

Android:CheckBox控件的更多相关文章

  1. android CheckBox控件的定义及事件监听

    http://www.beijibear.com/index.php?aid=336 android CheckBox控件的定义及事件监听,本例实现CheckBox控件的定义及点击事件的监听并显示结果 ...

  2. Android开发CheckBox控件,全选,反选,取消全选

    在Android开发中我们经常会使用CheckBox控件,那么怎么实现CheckBox控件的全选,反选呢 首先布局我们的界面: <?xml version="1.0" enc ...

  3. Android 基本控件相关知识整理

    Android应用开发的一项重要内容就是界面开发.对于用户来说,不管APP包含的逻辑多么复杂,功能多么强大,如果没有提供友好的图形交互界面,将很难吸引最终用户.作为一个程序员如何才能开发出友好的图形界 ...

  4. 一个Demo让你掌握Android所有控件

    原文:一个Demo让你掌握Android所有控件 本文是转载收藏,侵删,出处:"安卓巴士"      下面给出实现各个组件的源代码: 1.下拉框实现--Spinner packag ...

  5. android 基础控件(EditView、SeekBar等)的属性及使用方法

        android提供了大量的UI控件,本文将介绍TextView.ImageView.Button.EditView.ProgressBar.SeekBar.ScrollView.WebView ...

  6. Android基本控件之Menus

    在我们的手机中有很多样式的菜单,比如:我们的短信界面,每条短信,我们长按都会出现一个菜单,还有很多的种类.那么现在,我们就来详细的讨论一下安卓中的菜单 Android的控件中就有这么一个,叫做Menu ...

  7. checkBox控件的CheckedChanged与CheckedStateChanged区别

    Checked属性为bool类型,CheckState属性为枚举类型(CheckState.Checked.CheckState.Unchecked和CheckState.Indeterminate) ...

  8. Android:控件布局(相对布局)RelativeLayout

    RelativeLayout是相对布局控件:以控件之间相对位置或相对父容器位置进行排列. 相对布局常用属性: 子类控件相对子类控件:值是另外一个控件的id android:layout_above-- ...

  9. Android:控件布局(线性布局)LinearLayout

    LinearLayout是线性布局控件:要么横向排布,要么竖向排布 决定性属性:必须有的! android:orientation:vertical (垂直方向) .horizontal(水平方向) ...

  10. 矩阵, 矩阵 , Android基础控件之ImageView

    天下文章大家抄,以下所有内容,有来自copy,有来自查询,亦有自己的总结(目的是总结出自己的东西),所以说原创,不合适,说是转载也不恰当,所以我称之为笔记,可惜没有此分类选项,姑且不要脸一点,选择为原 ...

随机推荐

  1. 【iOS】Core Bluetooth

    本文介绍蓝牙4.0的一些基本知识. 基本概念.服务器.客户端 蓝牙LE是一个基于点对点的通信系统,其中一台设备作为服务器,另一台设备作为客户端.拥有数据的设备作为服务器,消费数据的设备作为客户端. 比 ...

  2. 【Python】 零碎知识积累 II

    [Python] 零碎知识积累 II ■ 函数的参数默认值在函数定义时确定并保存在内存中,调用函数时不会在内存中新开辟一块空间然后用参数默认值重新赋值,而是单纯地引用这个参数原来的地址.这就带来了一个 ...

  3. WEB端线上偶现问题如何复现?

    1.抓取出现问题的日志,还原操作过程,分析 每个过程中数据是否正常?是否有重复请求 2.询问当时操作员执行了哪些操作,尽可能多的了解事发经过 3.通过查看日志,数据库等信息,找到发生问题的节点, 比如 ...

  4. Java注解(1)-注解基础

    注解(Annotation)是在JAVA5中开始引入的,它为在代码中添加信息提供了一种新的方式.注解在一定程度上把元数据与源代码文件结合在一起,正如许多成熟的框架(Spring)所做的那样.那么,注解 ...

  5. ASP.NET没有魔法——ASP.NET MVC Razor与View渲染

    对于Web应用来说,它的界面是由浏览器根据HTML代码及其引用的相关资源进行渲染后展示给用户的结果,换句话说Web应用的界面呈现工作是由浏览器完成的,Web应用的原理是通过Http协议从服务器上获取到 ...

  6. Notepad++中实现Markdown语法高亮与实时预览

    Notepad ++是一个十分强大的编辑器,除了可以用来制作一般的纯文字说明文件,也十分适合编写计算机程序代码.Notepad ++不仅有语法高亮度显示,也有语法折叠功能,并且支持宏以及扩充基本功能的 ...

  7. Python+reuqests自动化接口测试

    1.最近自己在摸索Python+reuqests自动化接口测试,要实现某个功能,首先自己得有清晰的逻辑思路!这样效率才会很快! 思路--1.通过python读取Excel中的接口用例,2.通过pyth ...

  8. (译文)开始学习Vue——构建你的第一个Vue应用

    我们要构建如下组件:(最终代码在这里:https://codesandbox.io/s/38k1y8x375) 开始 Vue是支持单文件组件的,但是我们不准备这么做.你也可以构建一个全局的组件,通过V ...

  9. Property 'id' not found on type java.lang.String

    改为 忘写了$符,取不出来,因此报错!

  10. 利用1.1.1.1进行DNS网络加速,仅需2分钟让网络更快

    NEWS 近日,Cloudflare 和 APNIC联合推出了1.1.1.1DNS网络加速. Cloudflare 运行全球规模最大.速度最快的网络之一.APNIC 是一个非营利组织,管理着亚太和大洋 ...