无涯教程-Android - CheckBox函数

news2024/10/7 2:30:21

CheckBox是可以由用户切换的on/off开关。为用户提供一组互不排斥的可选选项时,应使用复选框。

CheckBox

CheckBox

复选框属性

以下是与CheckBox控件相关的重要属性。您可以查看Android官方文档以获取属性的完整列表以及可以在运行时更改这些属性的相关方法。

继承自 android.widget.TextView 类-

Sr.NoAttribute & 描述
1

android:autoText

如果设置,则指定此TextView具有文本输入法,并自动纠正一些常见的拼写错误。

2

android:drawableBottom

这是要在文本下方绘制的图形。

3

android:drawableRight

这是要在文本右侧绘制的图形。

4

android:editable

如果设置,则指定此TextView具有输入法。

5

android:text

这是要显示的文本。

继承自 android.view.View 类-

Sr.NoAttribute & 描述
1

android:background

这是一个可绘制的背景。

2

android:content描述

这定义了简短描述视图内容的文本。

3

android:id

这提供了该视图的标识符名称。

4

android:onClick

这是单击该视图时在该视图中要调用的方法的名称。

5

android:visibility

这控制了视图的初始可见性。

示例

本示例将带您完成简单的步骤,以展示如何使用Linear Layout和CheckBox创建自己的Android应用程序。

以下是修改后的主要Activity文件 src/MainActivity.java 的内容。

package com.example.myapplication;

import android.os.Bundle;
import android.app.Activity;
import android.widget.Button;

import android.view.View;
import android.view.View.OnClickListener;

import android.widget.CheckBox;
import android.widget.Toast;

public class MainActivity extends Activity {
   CheckBox ch1,ch2;
   Button b1,b2;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      
      ch1=(CheckBox)findViewById(R.id.checkBox1);
      ch2=(CheckBox)findViewById(R.id.checkBox2);
      
      b1=(Button)findViewById(R.id.button);
      b2=(Button)findViewById(R.id.button2);
      b2.setOnClickListener(new View.OnClickListener() {
         
         @Override
         public void onClick(View v) {
            finish();
         }
      });
      b1.setOnClickListener(new View.OnClickListener() {
         
         @Override
         public void onClick(View v) {
            StringBuffer result = new StringBuffer();
            result.append("Thanks : ").append(ch1.isChecked());
            result.append("\nThanks: ").append(ch2.isChecked());
            Toast.makeText(MainActivity.this, result.toString(), 
               Toast.LENGTH_LONG).show();
         }
      });
   }
}

以下是 res/layout/activity_main.xml 文件的内容-

<RelativeLayout 
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context=".MainActivity">
   
   <TextView
      android:id="@+id/textView1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Example of checkbox"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
      android:textSize="30dp" />
      
   <CheckBox
      android:id="@+id/checkBox1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Do you like Tutorials Point"
      android:layout_above="@+id/button"
      android:layout_centerHorizontal="true" />
      
   <CheckBox
      android:id="@+id/checkBox2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Do you like android "
      android:checked="false"
      android:layout_above="@+id/checkBox1"
      android:layout_alignLeft="@+id/checkBox1"
      android:layout_alignStart="@+id/checkBox1" />
   
   <TextView
      android:id="@+id/textView2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignLeft="@+id/checkBox1"
      android:layout_below="@+id/textView1"
      android:layout_marginTop="39dp"
      android:text="Tutorials point"
      android:textColor="#ff87ff09"
      android:textSize="30dp"
      android:layout_alignRight="@+id/textView1"
      android:layout_alignEnd="@+id/textView1" />
      
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Ok"
      android:id="@+id/button"
      android:layout_alignParentBottom="true"
      android:layout_alignLeft="@+id/checkBox1"
      android:layout_alignStart="@+id/checkBox1" />
      
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Cancel"
      android:id="@+id/button2"
      android:layout_alignParentBottom="true"
      android:layout_alignRight="@+id/textView2"
      android:layout_alignEnd="@+id/textView2" />
      
   <ImageButton
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/imageButton"
      android:src="@drawable/abc"
      android:layout_centerVertical="true"
      android:layout_centerHorizontal="true" />
      
</RelativeLayout>

以下是 res/values/strings.xml 的内容,以定义这些新常量-

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <string name="app_name">MyApplication</string>	
</resources>

以下是 AndroidManifest.xml 的默认内容-

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.myapplication" >
   
   <application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      
      <activity
         android:name="com.example.myapplication.MainActivity"
         android:label="@string/app_name" >
      
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      
      </activity>
      
   </application>
</manifest>

单击"运行Eclipse运行图标工具栏。 Android studio将应用程序安装在您的AVD上并启动它,如果设置和应用程序一切正常,它将在"Emulator"窗口中显示-

Android CheckBox Control

用户需要您选中"Do you like android"复选框还是"Do you like Tutorials Point"复选框。然后按"OK"按钮,如果所有处理均正确无误,则将显示消息,谢谢。否则请按取消按钮,它将关闭应用程序

Android 中的 CheckBox函数 - 无涯教程网无涯教程网提供CheckBox是可以由用户切换的on/off开关。为用户提供一组互不排斥的可选选项时,应使用...https://www.learnfk.com/android/android-checkbox-control.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/956979.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

探索IPv6:未来互联的新时代

文章目录 一、IPv4的问题二、IPv6的优势三、地址格式与地址书写压缩四、网段划分五、地址分类六、IPv6邻居发现协议七、常用命令 首先可以看下思维导图&#xff0c;以便更好的理解接下来的内容。 一、IPv4的问题 地址资源枯竭&#xff1a; 由于IPv4地址长度有限&#xff0c;可用…

导致事物失效的场景有哪些 ?

目录 1. 导致事物失效的场景有哪些 &#xff1f; 1.1 为什么 Transaction 修饰非 public 方法会导致事物失效 &#xff1f; 1.2 代码中使用 try/catch 处理了异常为什么会导致事物失效 &#xff1f; 1.3 为什么在类内部调用 Transaction 修饰的方法会导致事务失效 ? 1.4 …

一图胜千言!数据可视化多维讲解(Python)

数据聚合、汇总和可视化是支撑数据分析领域的三大支柱。长久以来&#xff0c;数据可视化都是一个强有力的工具&#xff0c;被业界广泛使用&#xff0c;却受限于 2 维。在本文中&#xff0c;作者将探索一些有效的多维数据可视化策略&#xff08;范围从 1 维到 6 维&#xff09;。…

批处理启动程序

&#x1f495;批处理启动程序 新建一个txt&#xff0c;把后缀改成bat&#xff0c;编辑脚本&#xff1a;start exe路径即可&#xff1a;

$nextTick使用

在Vue中&#xff0c;$nextTick是一个实例方法&#xff0c;用于在DOM更新之后执行回调函数。它可以用于在更新视图后执行一些操作&#xff0c;例如访问更新后的DOM元素或执行其他异步任务。 以下是$nextTick的使用方法&#xff1a; this.$nextTick(() > {// 在DOM更新后执行…

【若依框架RuoYi-Vue-Plus 图片回显不显示问题,OSS文件上传或者本地上传】

一、问题 1.设计表 product&#xff08;商品表&#xff09; 有 id &#xff08;id&#xff09; name&#xff08;商品名&#xff09;icon&#xff08;图标&#xff09; 2.使用若依代码生成功能&#xff0c;导入product表&#xff0c;代码生成。 3.将生成的代码导入到项目中得到…

Vue-Router 一篇搞定 Vue3

前言 在 Web 前端开发中&#xff0c;路由是非常重要的一环&#xff0c;但是路由到底是什么呢&#xff1f; 从路由的用途上讲 路由是指随着浏览器地址栏的变化&#xff0c;展示给用户不同的页面。 从路由的实现原理上讲 路由是URL到函数的映射。它将 URL 和应用程序的不同部分…

PXE 装机(五十)

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 目录 前言 一、PXE是什么 二、PXE的组件 三、配置vsftpd 四、配置tftp 五、准备pxelinx.0文件、引导文件、内核文件 ​六、配置dhcp 七、创建default文件 八、配置pxe无人值守…

C语言圣经KR笔记 1.10外部变量和作用域

1.10外部变量和作用域 上一节main中的变量&#xff0c;如line、longest等等&#xff0c;对main来说是私有的或者说是局部的。因为它们是在main中定义的&#xff0c;其他函数不能直接访问它们。其他函数中的变量也是如此&#xff0c;例如&#xff0c;getline中的变量 i 与copy中…

C语言:static关键字的使用

1.static修饰局部变量 这是static关键字使用最多的情况。我们知道局部变量是在程序运行阶段在栈上创建的&#xff0c;但是static修饰的局部变量是在程序编译阶段在代码段&#xff08;静态区&#xff09;创建的。所以在static修饰的变量所在函数执行结束后该变量依然存在。 //…

C++(17):标准库特殊设施

tuple 类型 tuple是类似pair的模板。 每个pair的成员类型都不相同&#xff0c;但每个 pair都恰好有两个成员。不同tuple类型的成员类型也不相同&#xff0c;但一个tuple可以有任意数量的成员。 每个确定的tuple类型的成员数目是固定的&#xff0c;但一个tuple类型的成员数目可…

OS 死锁处理

如果P先申请mutex 则mutex从1置零&#xff0c;假设申请到的empty 0则empty变成-1阻塞态 同理C中mutex从0变为-1&#xff0c;那么如果想离开阻塞态&#xff0c;那么就需要执行V&#xff08;empty&#xff09;但是如果执行V&#xff08;empty&#xff09;就需要P&#xff08;mu…

postgresql-窗口函数

postgresql-窗口函数 简介窗口函数的定义分区选项&#xff08;PARTITION BY&#xff09;排序选项&#xff08;ORDER BY&#xff09;窗口选项&#xff08;frame_clause&#xff09; 聚合窗口函数排名窗口函数演示了 CUME_DIST 和 NTILE 函数 取值窗口函数 简介 常见的聚合函数&…

飞致云开源社区月度动态报告(2023年8月)

自2023年6月起&#xff0c;中国领先的开源软件公司FIT2CLOUD飞致云以月度为单位发布《飞致云开源社区月度动态报告》&#xff0c;旨在向广大社区用户同步飞致云旗下系列开源软件的发展情况&#xff0c;以及当月主要的产品新版本发布、社区运营成果等相关信息。 飞致云开源大屏…

开讲啦!0基础也能玩转飞桨开源社区

作为cs/ai学生&#xff0c;你是否经历过这些至暗时刻&#xff1a; 希望快速入门深度学习&#xff0c;无奈网上到处都是看不懂“黑话”一遍遍计算综测小数点后四位&#xff0c;不断在保研边缘反复横跳看着“洁白如新”的履历叹气&#xff0c;一听到“考研复试”就头皮发麻“0实习…

【ES6】Promise.allSettled的用法

Promise.allSettled() 是一个Promise方法&#xff0c;用于处理一个Promise数组&#xff0c;返回一个新的Promise数组&#xff0c;每个元素对应原始Promise的状态。这个方法可以用于处理多个异步操作&#xff0c;并且能够获取每个操作的结果和状态。 下面是Promise.allSettled(…

基于蛇优化算法优化的BP神经网络(预测应用) - 附代码

基于蛇优化算法优化的BP神经网络&#xff08;预测应用&#xff09; - 附代码 文章目录 基于蛇优化算法优化的BP神经网络&#xff08;预测应用&#xff09; - 附代码1.数据介绍2.蛇优化优化BP神经网络2.1 BP神经网络参数设置2.2 蛇优化算法应用 4.测试结果&#xff1a;5.Matlab代…

ChatGPT~Error1015You are being rate limited

目录 问题背景 问题的原因 下来说说解决方案 总结 问题背景 今天使用Chatgpt的时候突然出现"You are being rate limited"的错误提示。 问题的原因 小问题了&#xff0c;又不是第一次被弄出来了&#xff0c;莫慌。 让我们先看看Chatgpt自己是怎么解释这个问题…

GE Diary 0001

今天是入职GEHC的第一天&#xff0c;虽然是OD岗位&#xff0c;但是员工氛围暂时没感觉有什么差异&#xff0c;领导也很随和。 公司印象&#xff1a;GEHC目前给我的印象是典型的外企&#xff1a;朝九晚五、无需打卡&#xff08;OD岗位需要打本公司的卡&#xff09;、短小精悍的会…

2781. 最长合法子字符串的长度

2781. 最长合法子字符串的长度 C代码&#xff1a;滑动窗口、哈希表 typedef struct{char* str;UT_hash_handle hh; } HashTable;HashTable* head;void AddToHash(char* str) {HashTable* out (HashTable*)malloc(sizeof(HashTable));out->str str;HASH_ADD_STR(head, str…