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. UI控件入门

    其实整个学习过程.确实有点儿混乱,因为不懂的东西太多,一会儿看这,一会儿看那,马上就乱了. 还是先做点儿简单的事儿,说控件,但不会说完全,只是入个门,知道怎么学控件,具体要学好每一个控件需要项目中磨练 ...

  2. iOS UITableView UIScrollView 的支持触摸事件

    在使用了 UITableView 或UIScrollView的controller 里无法响应触摸事件touch事件, 自定义tableView.scrollView #import <UIKi ...

  3. 转】启动tomcat时 错误: 代理抛出异常 : java.rmi.server.ExportException: Port already in use: 1099的解决办法

    原博文出自于:http://www.cnblogs.com/xdp-gacl/p/5288399.html   感谢! 一.问题描述 今天一来公司,在IntelliJ IDEA 中启动Tomcat服务 ...

  4. S3

    S3是Amazon EMR的一部分,它提供了一些Wikipedia的浏览统计数据,这些浏览数据的格式便于Spark测试.

  5. UVALive 5881 Unique Encryption Keys (DP)

    Unique Encryption Keys 题目链接: http://acm.hust.edu.cn/vjudge/problem/26633 Description http://7xjob4.c ...

  6. *** WARNING L1: UNRESOLVED EXTERNAL SYMBOL

    kei编译时提示: *** WARNING L1: UNRESOLVED EXTERNAL SYMBOL *** WARNING L1:reference made to unresolved ext ...

  7. win7 文件共享 xp

    前几天因为需要将win7内一文件夹共享给XP使用,因为NT5跟NT6安全机制的问题,共享的实现没有XP共享的方便,很多人是牺牲(关闭)了win7的系统防火墙才达到共享给XP的目的,但是关闭防火墙势必会 ...

  8. Python基础 数字、字符串、列表、元组、字典

    Number(数字)---> int.float.complex(复数) class int 在Python3中,整形(int)就是常见的整数:1,100,500,1000...... 浮点型( ...

  9. datagridview自动填充列头

    //填充datagridview dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

  10. Codeforces 583D. Once Again... (LIS变形)

    题目链接:http://codeforces.com/contest/583/problem/D 给你t个长度为n的数组.问你最长不下降子序列的长度. 一开始用第一个n数组的lis和最后一个n数组的l ...