多项选择(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>

阅读《Android 从入门到精通》(9)——多项选择的更多相关文章

  1. Android从入门到精通pdf+书源代码

    不须要积分,免费放送 Android从入门到精通的pdf,入门的好书籍,因为csdn文件大小的限制所以分成了两部分. part1地址:http://download.csdn.net/detail/a ...

  2. Android Volley入门到精通:使用Volley加载网络图片

    在上一篇文章中,我们了解了Volley到底是什么,以及它的基本用法.本篇文章中我们即将学习关于Volley更加高级的用法,如何你还没有看过我的上一篇文章的话,建议先去阅读Android Volley完 ...

  3. 阅读《Android 从入门到精通》(12)——自己主动完毕文本框

    自己主动完毕文本框(AutoCompleteTextView) java.lang.Object; android.view.View; android.view.TextView; android. ...

  4. 阅读《Android 从入门到精通》(17)——进度条

    进度条(ProgressBar) java.lang.Object; android.view.View; android.widget.ProgressBar; ProgressBar 类方法 Pr ...

  5. 阅读《Android 从入门到精通》(24)——切换图片

    切换图片(ImageSwitcher) java.lang.Object; android.view.View; android.widget.ViewGroup; android.widget.Fr ...

  6. 阅读《Android 从入门到精通》(33)——Intent 分类

    Intent 分类 显式 Intent:Intent("android.intent.action.CALL", Uri.parse("tel:" + stri ...

  7. 阅读《Android 从入门到精通》(15)——数字时钟

    数字时钟(DigitalClock) java.lang.Object; android.view.View; android.widget.TextView; android.widget.Digi ...

  8. 阅读《Android 从入门到精通》(10)——单项选择

    单项选择(RadioGroup) RadioGroup 是 LinearLayout 的子类,继承关系例如以下: android.view.ViewGroup android.widget.Linea ...

  9. 阅读《Android 从入门到精通》(29)——四大布局

    LinearLayout 类方法 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQ ...

随机推荐

  1. docker 容器间网络配置

    创建一个docker容器,docker系统会自动为该容器分配一个ip地址,通常是172.17开头. 我们可以在主机上用 docker inspect 命令 或者进入容器用ifconfig命令来查看容器 ...

  2. 利用svn log命令实现的资源版本更新

    无论页游或是手游都需要经常进行更新,而每一次更新几乎都是一部血泪吏.这里重点介绍一下前端资源打包的简化操作.目前2D手游主流都采用了cocos2d-x 绑lua的做法,因为lua相当于一种资源可以进行 ...

  3. 用layer-list实现图片旋转叠加、错位叠加、阴影、按钮指示灯

    先来看看一个简单的文件: <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:a ...

  4. Java Callable接口、Runable接口、Future接口

    1. Callable与Runable区别 Java从发布的第一个版本开始就可以很方便地编写多线程的应用程序,并在设计中引入异步处理.Thread类.Runnable接口和Java内存管理模型使得多线 ...

  5. Set a Room Mailbox to Show Details of a Meeting in its Calendar – Office 365

    You may notice that meetings with a ‘Room’ mailbox will by default only show a “Busy” status. Many, ...

  6. Ubuntu 虚拟机安装几点细节整理

    虚拟机或者Wubi安装其实都挺简单的,这里还是再次总结下,给遇到麻烦的同学一点参考. 虚拟机安装 虚拟机直接通过新建-标准-选择镜像,Vmware能够自动识别镜像并进行Easy Install安装,E ...

  7. 数学图形(1.47)贝塞尔(Bézier)曲线

    贝塞尔曲线又称贝兹曲线或贝济埃曲线,是由法国数学家Pierre Bézier所发现,由此为计算机矢量图形学奠定了基础.它的主要意义在于无论是直线或曲线都能在数学上予以描述. 上一节讲的是高次方程曲线, ...

  8. cesium原理篇(二)--网格划分【转】

    转自:http://www.cnblogs.com/fuckgiser/p/5772077.html 上一篇我们从宏观上介绍了Cesium的渲染过程,本章延续上一章的内容,详细介绍一下Cesium网格 ...

  9. xenapp 6.5 客户端插件第一次安装总是跳到官网

    部署完xenapp6.5后,在没有安装插件的客户端登录时,会出现“下载客户端插件”界面 其实网上已经有很多解决方案,大同小已,只是不知道为什么不适合我安装的版本而已.我安装时最新的版本xenapp 6 ...

  10. offsetof与container_of宏[总结]

    1.前言 今天在看代码时,遇到offsetof和container_of两个宏,觉得很有意思,功能很强大.offsetof是用来判断结构体中成员的偏移位置,container_of宏用来根据成员的地址 ...