阅读《Android 从入门到精通》(9)——多项选择
来源:互联网 发布:淘宝网电脑主板 编辑:程序博客网 时间:2024/05/16 07:54
多项选择(CheckBox)
CheckBox 类是 Button 的子类,层次关系如下:
android.widget.Button
android.widget.CompoundButton
android.widget.CheckBox
CheckBox 类方法
CheckBox 示例
完整工程:http://download.csdn.net/detail/sweetloveft/9400761
下述程序中,主要学习 CheckBox 和 Toast 的用法,CheckBox 作为复选框,选中未选中只有 true 和 false 的数值,必须添加具体的监听事件,才能确保其在状态改变时执行对应动作,而 Toast 通知的作用是创建一个浮动文本,浮于文字上方,显现一段时间后退出,需要注意这个延时是累积的,同时快速触发多个 Toast 时,每个 Toast 必须在前者显示结束后才能显示
此外要学习下 Toast 的 Gravity 位置有哪些,要知道以下内容,其中 setGravity 中,x > 0 右移反之左移,y > 0 上移反之下移。
下述程序中,主要学习 CheckBox 和 Toast 的用法,CheckBox 作为复选框,选中未选中只有 true 和 false 的数值,必须添加具体的监听事件,才能确保其在状态改变时执行对应动作,而 Toast 通知的作用是创建一个浮动文本,浮于文字上方,显现一段时间后退出,需要注意这个延时是累积的,同时快速触发多个 Toast 时,每个 Toast 必须在前者显示结束后才能显示
此外要学习下 Toast 的 Gravity 位置有哪些,要知道以下内容,其中 setGravity 中,x > 0 右移反之左移,y > 0 上移反之下移。
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;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.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 eventsfor (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 {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {// TODO Auto-generated method stubString text = buttonView.getText().toString();if (isChecked)text += " 被选择";elsetext += " 取消选择";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>
0 0
- 阅读《Android 从入门到精通》(9)——多项选择
- 阅读《Android 从入门到精通》(10)——单项选择
- 阅读《Android 从入门到精通》(1)——了解 Android 开发
- 阅读《Android 从入门到精通》(2)——熟悉开发环境
- 阅读《Android 从入门到精通》(3)——Activity 与界面
- 阅读《Android 从入门到精通》(4)——人机界面常用组件概述
- 阅读《Android 从入门到精通》(5)——文本框视图
- 阅读《Android 从入门到精通》(?)——View 和 ViewGroup
- 阅读《Android 从入门到精通》(6)——按钮
- 阅读《Android 从入门到精通》(7)——图片按钮
- 阅读《Android 从入门到精通》(8)——编辑框
- 阅读《Android 从入门到精通》(11)——下拉列表
- 阅读《Android 从入门到精通》(12)——自动完成文本框
- 阅读《Android 从入门到精通》(13)——日期选择器
- 阅读《Android 从入门到精通》(14)——时间选择器
- 阅读《Android 从入门到精通》(15)——数字时钟
- 阅读《Android 从入门到精通》(16)——表状时钟
- 阅读《Android 从入门到精通》(17)——进度条
- Java 中 StringBuilder 在高性能用法总结
- Spring AOP 的实现
- block的理解与 MRC,ARC下内存管理
- 控制文件丢失后,进行数据库恢复的过程
- 【SpringMVC】文件上传Expected MultipartHttpServletRequest: is a MultipartResolver错误解决
- 阅读《Android 从入门到精通》(9)——多项选择
- OpenGL: 矩阵相乘的顺序
- 数据库
- 基于B-树的图书管理系统课程设计
- 让程序在崩溃时体面的退出之SEH
- [JZOJ3248] Type
- MapReduce 图解流程超详细解答(1)-【map阶段】
- POJ 1005_I Think I Need a Houseboat
- Java中HashMap和TreeMap的区别深入理解