多项选择(CheckBox)

CheckBox 类是 Button 的子类,层次关系例如以下:

android.widget.Button
android.widget.CompoundButton
android.widget.CheckBox

CheckBox 类方法

CheckBox 演示样例

完整project:http://download.csdn.net/detail/sweetloveft/9400761
下述程序中。主要学习 CheckBox 和 Toast 的使用方法,CheckBox 作为复选框,选中未选中仅仅有 true 和 false 的数值,必须加入详细的监听事件,才干确保其在状态改变时运行相应动作。而 Toast 通知的作用是创建一个浮动文本,浮于文字上方,显现一段时间后退出。须要注意这个延时是累积的,同一时候高速触发多个 Toast 时,每一个 Toast 必须在前者显示结束后才干显示
此外要学习下 Toast 的 Gravity 位置有哪些。要知道下面内容,当中 setGravity 中,x > 0 右移反之左移。y > 0 上移反之下移。

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

1.MainActivity.java

package com.sweetlover.activity;

import com.sweetlover.checkboxdemo.R;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast; public class MainActivity extends Activity { private static final int CTRL_NUM = 4; private CheckBox[] checkBox = null; @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); checkBox = new CheckBox[CTRL_NUM];
checkBox[0] = (CheckBox)findViewById(R.id.checkBox1);
checkBox[1] = (CheckBox)findViewById(R.id.checkBox2);
checkBox[2] = (CheckBox)findViewById(R.id.checkBox3);
checkBox[3] = (CheckBox)findViewById(R.id.checkBox4); // TODO Register events
for (int i = 0; i < CTRL_NUM; i++)
checkBox[i].setOnCheckedChangeListener(new CheckBoxListener());
} public void onClickSubmit(View view) {
String str = "";
for (int i = 0; i < CTRL_NUM; i++) {
if (checkBox[i].isChecked())
str += checkBox[i].getText() + " ";
}
Toast.makeText(this, str + "被选择", Toast.LENGTH_LONG).show();
} class CheckBoxListener implements OnCheckedChangeListener { @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
String text = buttonView.getText().toString();
if (isChecked)
text += " 被选择";
else
text += " 取消选择";
Toast toast = Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 5, 5);
toast.show();
}
}
}

2.activity_main.xml

<?

xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="30dp" > <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/Tittle"
android:textAppearance="?android:attr/textAppearanceMedium" /> <CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:text="@string/Profile1" /> <CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:text="@string/Profile2" /> <CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:text="@string/Profile3" /> <CheckBox
android:id="@+id/checkBox4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:text="@string/Profile4" /> <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:onClick="onClickSubmit"
android:text="@string/Submit" /> </LinearLayout>

3.string.xml

<resources>

    <string name="app_name">CheckBoxDemo</string>
<string name="Tittle">请选择喜欢的情景模式</string>
<string name="Profile1">上班模式</string>
<string name="Profile2">家庭模式</string>
<string name="Profile3">旅游模式</string>
<string name="Profile4">会议模式</string>
<string name="Submit">提交</string> </resources>

4.AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sweetlover.checkboxdemo"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity android:name="com.sweetlover.activity.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application> </manifest>

最新文章

  1. 让你从零开始学会写爬虫的5个教程(Python)
  2. [Keras] Develop Neural Network With Keras Step-By-Step
  3. PHP 爬虫
  4. qsort函数详解
  5. 【转】IOS设备旋转的内部处理流程以及一些【优化建议】
  6. c# 计算两日期的工作时间间隔(排除非工作日)及计算下一个工作时间点.
  7. 解决:javah 无法访问引用Android对象的问题
  8. poj 3349 (最小表示法)
  9. 转:socket通信简介
  10. ssh命令
  11. poj 1456 Supermarket(并查集维护区间)
  12. CSS3+HTML5特效5 - 震动的文字
  13. 关于ARM内核与架构的解释
  14. css中的几个小tip(一)
  15. UGUI表情系统&amp;超链接解决方案
  16. Zip4J最简单用法
  17. 使用 Parallels Destop 最小化安装 centOS 操作系统
  18. linux中 bashrc文件的alias添加快捷命令
  19. lucene 一次查询多个id
  20. 10.mysql-触发器.md

热门文章

  1. tomcat使用不同jdk的解决方法
  2. git 拉取远程指定分支 pull本地不存在的分支
  3. 好汉两个半第一季/全集Two And A Half Men迅雷下载
  4. activity启动流程速记笔记
  5. Java并发编程的艺术(九)——批量获取多条线程的执行结果
  6. Docker存出载入镜像
  7. HTML5 &lt;Audio&gt;标签API整理(三)
  8. 带标签的 break 和 continue (Java)
  9. iOS开发-UITapGestureRecognizer手势
  10. ElasticSearch客户端注解使用介绍