阅读《Android 从入门到精通》(9)——多项选择
多项选择(CheckBox)
CheckBox 类是 Button 的子类,层次关系例如以下:
android.widget.Button
android.widget.CompoundButton
android.widget.CheckBox
CheckBox 类方法
CheckBox 演示样例
下述程序中。主要学习 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)——多项选择的更多相关文章
- Android从入门到精通pdf+书源代码
不须要积分,免费放送 Android从入门到精通的pdf,入门的好书籍,因为csdn文件大小的限制所以分成了两部分. part1地址:http://download.csdn.net/detail/a ...
- Android Volley入门到精通:使用Volley加载网络图片
在上一篇文章中,我们了解了Volley到底是什么,以及它的基本用法.本篇文章中我们即将学习关于Volley更加高级的用法,如何你还没有看过我的上一篇文章的话,建议先去阅读Android Volley完 ...
- 阅读《Android 从入门到精通》(12)——自己主动完毕文本框
自己主动完毕文本框(AutoCompleteTextView) java.lang.Object; android.view.View; android.view.TextView; android. ...
- 阅读《Android 从入门到精通》(17)——进度条
进度条(ProgressBar) java.lang.Object; android.view.View; android.widget.ProgressBar; ProgressBar 类方法 Pr ...
- 阅读《Android 从入门到精通》(24)——切换图片
切换图片(ImageSwitcher) java.lang.Object; android.view.View; android.widget.ViewGroup; android.widget.Fr ...
- 阅读《Android 从入门到精通》(33)——Intent 分类
Intent 分类 显式 Intent:Intent("android.intent.action.CALL", Uri.parse("tel:" + stri ...
- 阅读《Android 从入门到精通》(15)——数字时钟
数字时钟(DigitalClock) java.lang.Object; android.view.View; android.widget.TextView; android.widget.Digi ...
- 阅读《Android 从入门到精通》(10)——单项选择
单项选择(RadioGroup) RadioGroup 是 LinearLayout 的子类,继承关系例如以下: android.view.ViewGroup android.widget.Linea ...
- 阅读《Android 从入门到精通》(29)——四大布局
LinearLayout 类方法 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQ ...
随机推荐
- Mac环境下配置Tomcat+Eclipse
下载Tomcat 首先在 Tomcat官方网站 找到自己合适的版本,下载 tar.gz 版本的,下载完成后解压缩到一个目录,进入这个目录下的 bin 执行 startup.sh,如果看到下面的界面,表 ...
- pkg-config原理及用法
原文 https://blog.csdn.net/luotuo44/article/details/24836901 我们在用第三方库的时候,经常会用到pkg-config这个东西来编译程序.那pk ...
- [转]微信JSAPI 微信内置JSAPI 2015年1月官方正式API接口,分享完整实例
FROM : http://www.oschina.net/code/snippet_2276613_45290 HTML通过微信,分享朋友圈出发此JSAPI <?php require_onc ...
- [转]11个在线编码大赛,与全球程序员PK
From : http://news.cnblogs.com/n/187196/ 英文原文:10 Online Coding Contests For Programmers! 如果你拥有出色的编码技 ...
- ss简单使用
ss简单使用 ss即socket state. 1.常用语句 ss -l 显示所有处于监听的网络接口连接 ss -pl 显示所有处于监听的网络接口连接,及相应的进程名称.进号等 ss -t -a 显示 ...
- Spring框架中IoC(控制反转)的原理(转)
原文链接:Spring框架中IoC(控制反转)的原理 一.IoC的基础知识以及原理: 1.IoC理论的背景:在采用面向对象方法设计的软件系统中,底层实现都是由N个对象组成的,所有的对象通过彼此的合作, ...
- Unhandled Exception: System.BadImageFormatException: Could not load file or assembly (2008R2配置x64website)
.NET Error Message: Unhandled Exception: System.BadImageFormatException: Could not load file or asse ...
- Java系列:JVM中的OopMap(zz)
调用栈里的引用类型数据是GC的根集合(root set)的重要组成部分:找出栈上的引用是GC的根枚举(root enumeration)中不可或缺的一环. JVM选择用什么方式会影响到GC的实现: 如 ...
- RxJava【变换】操作符 map flatMap concatMap buffer MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- SciPy 安装不上?
参考:链接:https://www.zhihu.com/question/30188492/answer/150928275来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处 ...