阅读《Android 从入门到精通》(9)——多项选择

2024-03-01 02:38

本文主要是介绍阅读《Android 从入门到精通》(9)——多项选择,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

多项选择(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 上移反之下移。

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" ><TextViewandroid: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" /><CheckBoxandroid: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" /><CheckBoxandroid: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" /><CheckBoxandroid: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" /><CheckBoxandroid: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" /><Buttonandroid: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-sdkandroid:minSdkVersion="8"android:targetSdkVersion="19" /><applicationandroid: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)——多项选择的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/760937

相关文章

Android使用java实现网络连通性检查详解

《Android使用java实现网络连通性检查详解》这篇文章主要为大家详细介绍了Android使用java实现网络连通性检查的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录NetCheck.Java(可直接拷贝)使用示例(Activity/Fragment 内)权限要求

SpringCloud Stream 快速入门实例教程

《SpringCloudStream快速入门实例教程》本文介绍了SpringCloudStream(SCS)组件在分布式系统中的作用,以及如何集成到SpringBoot项目中,通过SCS,可... 目录1.SCS 组件的出现的背景和作用2.SCS 集成srping Boot项目3.Yml 配置4.Sprin

2025最新版Android Studio安装及组件配置教程(SDK、JDK、Gradle)

《2025最新版AndroidStudio安装及组件配置教程(SDK、JDK、Gradle)》:本文主要介绍2025最新版AndroidStudio安装及组件配置(SDK、JDK、Gradle... 目录原生 android 简介Android Studio必备组件一、Android Studio安装二、A

SpringMVC配置、映射与参数处理​入门案例详解

《SpringMVC配置、映射与参数处理​入门案例详解》文章介绍了SpringMVC框架的基本概念和使用方法,包括如何配置和编写Controller、设置请求映射规则、使用RestFul风格、获取请求... 目录1.SpringMVC概述2.入门案例①导入相关依赖②配置web.XML③配置SpringMVC

MySQL索引踩坑合集从入门到精通

《MySQL索引踩坑合集从入门到精通》本文详细介绍了MySQL索引的使用,包括索引的类型、创建、使用、优化技巧及最佳实践,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友... 目录mysql索引完整教程:从入门到入土(附实战踩坑指南)一、索引是什么?为什么需要它?1.1 什么

Java Lettuce 客户端入门到生产的实现步骤

《JavaLettuce客户端入门到生产的实现步骤》本文主要介绍了JavaLettuce客户端入门到生产的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 目录1 安装依赖MavenGradle2 最小化连接示例3 核心特性速览4 生产环境配置建议5 常见问题

Android实现图片浏览功能的示例详解(附带源码)

《Android实现图片浏览功能的示例详解(附带源码)》在许多应用中,都需要展示图片并支持用户进行浏览,本文主要为大家介绍了如何通过Android实现图片浏览功能,感兴趣的小伙伴可以跟随小编一起学习一... 目录一、项目背景详细介绍二、项目需求详细介绍三、相关技术详细介绍四、实现思路详细介绍五、完整实现代码

在Android中使用WebView在线查看PDF文件的方法示例

《在Android中使用WebView在线查看PDF文件的方法示例》在Android应用开发中,有时我们需要在客户端展示PDF文件,以便用户可以阅读或交互,:本文主要介绍在Android中使用We... 目录简介:1. WebView组件介绍2. 在androidManifest.XML中添加Interne

从入门到精通详解Python虚拟环境完全指南

《从入门到精通详解Python虚拟环境完全指南》Python虚拟环境是一个独立的Python运行环境,它允许你为不同的项目创建隔离的Python环境,下面小编就来和大家详细介绍一下吧... 目录什么是python虚拟环境一、使用venv创建和管理虚拟环境1.1 创建虚拟环境1.2 激活虚拟环境1.3 验证虚

Android协程高级用法大全

《Android协程高级用法大全》这篇文章给大家介绍Android协程高级用法大全,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友跟随小编一起学习吧... 目录1️⃣ 协程作用域(CoroutineScope)与生命周期绑定Activity/Fragment 中手