1. Custom String

打开 “res/values/strings.xml” 文件,

File : res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources> <string name="app_name">CheckBoxDemo</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="chk_ios">IPhone</string>
<string name="chk_android">Android</string>
<string name="chk_windows">Windows Mobile</string>
<string name="btn_display">Display</string>
</resources>

2. CheckBox

打开 “res/layout/activity_main.xml” 文件, 

File : res/layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <CheckBox
android:id="@+id/chkIos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/chk_ios" /> <CheckBox
android:id="@+id/chkAndroid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/chk_android"
android:checked="true" /> <CheckBox
android:id="@+id/chkWindows"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/chk_windows" /> <Button
android:id="@+id/btnDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_display" /> </LinearLayout>

注: android:checked="true"   表示默认选中。本例子中,chkAndroid默认选中

3. Code Code

package com.jiangge.checkboxdemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast; public class MainActivity extends Activity { private CheckBox chkIos, chkAndroid, chkWindows;
private Button btnDisplay; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); addListenerOnChkIos();
addListenerOnButton();
} private void addListenerOnChkIos() {
chkIos = (CheckBox) findViewById(R.id.chkIos); chkIos.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// chkIos 是否被选中
if (((CheckBox)v).isChecked()) {
Toast.makeText(getApplicationContext(), "Bro try android,哈哈",Toast.LENGTH_LONG).show();
}
}
});
} private void addListenerOnButton() {
chkIos = (CheckBox) findViewById(R.id.chkIos);
chkAndroid = (CheckBox) findViewById(R.id.chkAndroid);
chkWindows = (CheckBox) findViewById(R.id.chkWindows);
btnDisplay = (Button) findViewById(R.id.btnDisplay); btnDisplay.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
StringBuffer result = new StringBuffer();
result.append("IPhone check : ").append(chkIos.isChecked());
result.append("\nAndroid check : ").append(chkAndroid.isChecked());
result.append("\nWindows Mobile check :").append(chkWindows.isChecked()); Toast.makeText(MainActivity.this, result.toString(),
Toast.LENGTH_LONG).show();
}
}); } }

注:

if (((CheckBox)v).isChecked())  向下转型。

StringBuffer result = new StringBuffer();

result .append().append()

4、运行结果:

一启动时的运行结果:

选中 IPhone (和Android)的运行结果:

选中IPone 和  Winows Mobile,然后点击 Button 按钮的运行结果:

Android Checkbox Example的更多相关文章

  1. xamarin android checkbox自定义样式

    xamarin android checkbox自定义样式 在drawable文件在新建checkbox_bg.xml文件 <?xml version="1.0" encod ...

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

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

  3. android CheckBox的运用

    CheckBox定义一个同意协议的按钮,只要同意button才可以点击 XML代码 <CheckBox android:id="@+id/checkbox1" android ...

  4. Android checkbox 自定义点击效果

    安卓默认的效果                         自定义后的效果 前面的图片当然可以自己修改. 实现这个效果的步骤如下 1.建立 一个selector 的xml <?xml ver ...

  5. Android checkBox

    checkBox      状态:选中(true),未选中(false)      属性:           checked="true/false"; private Chec ...

  6. Android checkbox和radiobutton 以及Toast和AlertDialog的使用

    package com.example.radiobutton_01; import android.app.Activity; import android.os.Bundle; import an ...

  7. android CheckBox RadioButton 照片和文字的间距问题

    利用自身的定义CheckBox 要么RadioButton时间.定义自己的图标和文字在不同的手机显示不同的音高.有时不太好控制,下面是我自己的定义CheckBox: 在Layout在下面xml: &l ...

  8. android checkbox 未选中状态 已选中状态 替换成自己的图片

    效果图: 未选中状态: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA ...

  9. android CheckBox与监听

    <CheckBox  android:id="@+id/cb1"  android:layout_width="fill_parent"  android ...

随机推荐

  1. STL六大组件之——迭代器这个东西

    迭代器:除了在其它语言中司空见惯的下标法访问容器元素之外,C++语言提供了一种全新的方法——迭代器(iterator)来访问容器的元素.迭代器其实类似于引用,指向容器中某一元素.换个方式来说,容器就是 ...

  2. Android的主要组件

    (一)Activity(活动) 作用:提供可视化用户界面 只能通过setContentView(View)来显示指定组件 View组件是所有UI控件.容器控件的基类,View组件就是android应用 ...

  3. GRIDVIEW传值的两种方法

    第一种方法:在onselectedindexchanged中写事件 第二种方法:OnRowCommand事件 前端页面: <%@ Page Language="C#" Aut ...

  4. Linux-sed用法

    本文为转载,原地址:http://www.cnblogs.com/dong008259/archive/2011/12/07/2279897.html sed是一个很好的文件处理工具,本身是一个管道命 ...

  5. dom 左右两侧得广告(兼容IE FF)

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  6. 原生JS默认设置默认值的写法

    json=json||{};json.type=json.type||'get';json.data=json.data||{};json.time=json.time||2000;

  7. 第二百二十七天 how can I 坚持

    今天去了蟒山,天池,刚去的时候身体有点难受,整天都是那样,回来就好多了,不知道怎么回事. 天池竟然是个人造池,挺大,没有去十三陵,回来都很晚了. 去天池竟然是走的小路,已经关了,不让进,里边玲玲清清的 ...

  8. Linux下Python获取IP地址

    <lnmp一键安装包>中需要获取ip地址,有2种情况:如果服务器只有私网地址没有公网地址,这个时候获取的IP(即私网地址)不能用来判断服务器的位置,于是取其网关地址用来判断服务器在国内还是 ...

  9. binarySearch二分查找——Javascript实现

    在很早之前,我就写过了一篇也关于二分法的相关博文:JavaScript快排与原生sort的测试.当时是用二分法进行快速排序,其实和这次思路大致相当.二分查找最重要的一个条件,就是需要将数组先按照从小到 ...

  10. UVALive 5880 Vigenère Cipher Encryption (模拟)

    Stack Machine Executor 题目链接: http://acm.hust.edu.cn/vjudge/problem/26628 Description http://7xjob4.c ...