当前位置: 首页 > news >正文

安卓(android)实现注册界面【Android移动开发基础案例教程(第2版)黑马程序员】

一、实验目的(如果代码有错漏,可查看源码)

1.掌握LinearLayout、RelativeLayout、FrameLayout等布局的综合使用。
2.掌握ImageView、TextView、EditText、CheckBox、Button、RadioGroup、RadioButton、ListView、RecyclerView等控件在项目中的综合使用。
3.掌握布局和控件的灵活运用,实现注册界面的布局设计。

二、实验条件

1.掌握了常见界面布局的特点及使用,熟悉XML方式和java代码方式编写界面布局。
2.掌握了ImageView、TextView、EditText、CheckBox、Button、RadioGroup、RadioButton等简单控件的使用。

三、实验内容

1.去掉默认标题栏:修改theme属性的值去掉默认标题栏。
2.实现注册功能: 获取界面控件创建文本样式;设置单选按钮的点击事件。
3.运行结果:运行程序,并在界面上输入注册信息;点击“提交”按钮,Toast提示注册成功。

四、实验指导

1.去掉默认标题栏

(1)依次进入创建项目下的app目录→src目录→main目录→res目录→values目录。

(2)打开themes.xml文件,修改应用的主题如下:

<style name="Theme.RegiserAppDemo" parent="Theme.AppCompat.NoActionBar"><!-- Primary brand color. --><!-- Secondary brand color. --><!-- Status bar color. -->……<!-- Customize your theme here. -->
</style>

2.实现注册功能

(1)在value目录下创建styles.xml文件,定义界面中水平分割线、垂直分割线、TextView和EditText等控件的样式如下:

<?xml version="1.0" encoding="utf-8"?>
<resources><style name="hLine"><item name="android:layout_width">match_parent</item><item name="android:layout_height">1dp</item><item name="android:background">@android:color/white</item></style><style name="tvOne"><item name="android:layout_width">0dp</item><item name="android:layout_height">match_parent</item><item name="android:layout_weight">1</item><item name="android:drawablePadding">8dp</item><item name="android:gravity">center_horizontal</item><item name="android:paddingTop">40dp</item><item name="android:textColor">@android:color/white</item><item name="android:textSize">15dp</item></style><style name="tvTwo"><item name="android:layout_width">wrap_content</item><item name="android:layout_height">wrap_content</item><item name="android:layout_marginLeft">20dp</item><item name="android:textColor">@android:color/white</item><item name="android:textSize">15dp</item></style><style name="etOne"><item name="android:layout_width">match_parent</item><item name="android:layout_height">wrap_content</item><item name="android:layout_marginLeft">30dp</item><item name="android:background">@null</item><item name="android:textColor">@android:color/white</item></style></resources>

(2)在layout目录下的layout_main.xml文件中,引入界面需要的布局和控件,将第(1)步的样式文件引入设置,布局代码结构、代码图及设计预览图如下:

<?xml version="1.0" encoding="utf-8"?>
<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:background="@drawable/background_bg"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:ignore="ExtraText"><TextViewandroid:id="@+id/tv_title"android:layout_width="match_parent"android:layout_height="50dp"android:background="@android:color/holo_blue_bright"android:gravity="center"android:text="注册"android:textColor="@android:color/white"android:textSize="20sp" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="130dp"android:orientation="horizontal"android:divider="@android:color/white"android:showDividers="middle"><TextViewstyle="@style/tvOne"android:drawableTop="@drawable/qq_icon"android:text="用QQ注册" /><TextViewstyle="@style/tvOne"android:drawableTop="@drawable/weixin_icon"android:text="用微信注册" /></LinearLayout><View style="@style/hLine" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:orientation="horizontal"android:padding="15dp"><ImageViewandroid:layout_width="wrap_content"android:layout_height="match_parent"android:src="@drawable/email_icon" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="15dp"android:text="使用电子邮箱注册"android:textColor="@android:color/white"android:textSize="15sp" /></LinearLayout><View style="@style/hLine" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:padding="15dp"><TextViewstyle="@style/tvTwo"android:text="名字" /><EditTextandroid:id="@+id/et_name"style="@style/etOne" /></LinearLayout><View style="@style/hLine" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:padding="15dp"><TextViewstyle="@style/tvTwo"android:text="邮箱" /><EditTextandroid:id="@+id/et_email"style="@style/etOne"android:inputType="textEmailAddress" /></LinearLayout><View style="@style/hLine" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:padding="15dp"><TextViewstyle="@style/tvTwo"android:text="密码" /><EditTextandroid:id="@+id/et_pwd"style="@style/etOne"android:inputType="textPassword" /></LinearLayout><View style="@style/hLine" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:padding="15dp"><TextViewstyle="@style/tvTwo"android:text="性别" /><RadioGroupandroid:id="@+id/rg_sex"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_marginLeft="50dp"android:orientation="horizontal"><RadioButtonandroid:id="@+id/rb_boy"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="男"android:textColor="@android:color/white"android:textSize="15sp" /><RadioButtonandroid:id="@+id/rb_girl"style="@style/tvTwo"android:layout_width="match_parent"android:text="女" /></RadioGroup></LinearLayout><View style="@style/hLine" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:padding="15dp"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="请选择兴趣爱好:"android:textColor="@android:color/white"android:textSize="15sp" /><CheckBoxandroid:id="@+id/cb_sing"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="唱歌"android:textColor="@android:color/white"android:textSize="15sp" /><CheckBoxandroid:id="@+id/cb_dance"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="跳舞"android:textColor="@android:color/white"android:textSize="15sp" /><CheckBoxandroid:id="@+id/cb_read"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="读书"android:textColor="@android:color/white"android:textSize="15sp" /></LinearLayout></LinearLayout><View style="@style/hLine" /><Viewandroid:id="@+id/v_line"android:layout_width="match_parent"android:layout_height="1dp"android:layout_above="@+id/btn_submit"android:background="@android:color/darker_gray" /><Buttonandroid:id="@+id/btn_submit"android:layout_width="match_parent"android:layout_height="50dp"android:layout_alignParentBottom="true"android:background="@android:color/transparent"android:gravity="center"android:text="提交"android:textColor="@android:color/white"android:textSize="18sp" /></RelativeLayout>

(3)在MainActivity.java文件中实现需求功能业务逻辑,代码如下:

package com.example.myapplication2022;import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;import androidx.appcompat.app.AppCompatActivity;public class MainActivity extends AppCompatActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {private EditText et_name, et_email, et_pwd;private Button btn_submit;private String name, email, pwd, sex, hobbies;private RadioGroup rg_sex;private CheckBox cb_sing, cb_dance, cb_read;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.layout_main);init();}private void init() {// 初始化控件et_name = findViewById(R.id.et_name);et_email = findViewById(R.id.et_email);et_pwd = findViewById(R.id.et_pwd);rg_sex = findViewById(R.id.rg_sex);cb_sing = findViewById(R.id.cb_sing);cb_dance = findViewById(R.id.cb_dance);cb_read = findViewById(R.id.cb_read);btn_submit = findViewById(R.id.btn_submit);// 设置监听事件btn_submit.setOnClickListener(this);cb_sing.setOnCheckedChangeListener(this);cb_dance.setOnCheckedChangeListener(this);cb_read.setOnCheckedChangeListener(this);hobbies = new String();// 设置性别选项的监听事件rg_sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {switch (checkedId) {case R.id.rb_boy:sex = "男";break;case R.id.rb_girl:sex = "女";break;}}});}/*** 获取用户输入的信息*/private void getData() {name = et_name.getText().toString().trim();email = et_email.getText().toString().trim();pwd = et_pwd.getText().toString().trim();}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.btn_submit:getData();if (TextUtils.isEmpty(name)) {Toast.makeText(MainActivity.this, "请输入名字", Toast.LENGTH_SHORT).show();} else if (TextUtils.isEmpty(email)) {Toast.makeText(MainActivity.this, "请输入邮箱", Toast.LENGTH_SHORT).show();} else if (TextUtils.isEmpty(pwd)) {Toast.makeText(MainActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();} else if (TextUtils.isEmpty(sex)) {Toast.makeText(MainActivity.this, "请选择性别", Toast.LENGTH_SHORT).show();} else if (TextUtils.isEmpty(hobbies)) {Toast.makeText(MainActivity.this, "请选择兴趣爱好", Toast.LENGTH_SHORT).show();} else {Toast.makeText(MainActivity.this, "注册成功", Toast.LENGTH_SHORT).show();Log.i("MainActivity", "注册的用户信息:" +"名字:" + name +",邮箱:" + email +",性别:" + sex +",兴趣爱好:" + hobbies);}break;}}/*** 监听兴趣爱好的点击事件*/@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {String motion = buttonView.getText().toString(); // 获取选项的文本内容if (isChecked) {if (!hobbies.contains(motion)) {// 判断之前选择的内容是否与此次选择的不同hobbies = hobbies + motion;}} else {if (hobbies.contains(motion)) {hobbies = hobbies.replace(motion, ""); // 取消选择时移除}}}
}

3.运行结果

五、代码下载地址:

android: 实现注册界面、实现注册界面、饭堂小广播、音乐播放器、记事本、读取手机通讯录、学生管理系统 - Gitee.com

https://download.csdn.net/download/m0_63755691/90327318 

相关文章:

安卓(android)实现注册界面【Android移动开发基础案例教程(第2版)黑马程序员】

一、实验目的&#xff08;如果代码有错漏&#xff0c;可查看源码&#xff09; 1.掌握LinearLayout、RelativeLayout、FrameLayout等布局的综合使用。 2.掌握ImageView、TextView、EditText、CheckBox、Button、RadioGroup、RadioButton、ListView、RecyclerView等控件在项目中的…...

【 AI agents】letta:2024年代理堆栈演进(中英文翻译)

The AI agents stack AI 代理堆栈 November 14, 2024 11月 14, 2024原文: The AI agents stack官方教程教程学习笔记: 【memgpt】letta 课程1/2:从头实现一个自我编辑、记忆和多步骤推理的代理Understanding the AI agents landscape 了解 AI 代理环境 Although we see a …...

Java中 instanceof 的用法(详解)

目录 引言 基本语法 基本作用 1. 检查对象是否是指定类的实例 2. 检查对象是否是子类的实例 3. 检查对象是否实现某个接口 4.null 处理 错误分析&#xff1a; 5.综合对比示例 最后总结 注意事项 引言 instanceof 概念在多态中引出&#xff0c;因为在多态发生时&…...

联想拯救者R720笔记本外接显示屏方法,显示屏是2K屏27英寸

晚上23点10分前下单&#xff0c;第二天上午显示屏送到&#xff0c;检查外包装没拆封过。这个屏幕左下方有几个按键&#xff0c;按一按就开屏幕、按一按就关闭屏幕&#xff0c;按一按方便节省时间&#xff0c;也支持阅读等模式。 显示屏是 &#xff1a;AOC 27英寸 2K高清 100Hz…...

【RocketMQ 存储】- 一文总结 RocketMQ 的存储结构-基础

文章目录 1. 前言 本文章基于 RocketMQ 4.9.3 1. 前言 RocketMQ 存储部分系列文章&#xff1a; 【RocketMQ 存储】- RocketMQ存储类 MappedFile 【RocketMQ 存储】- 一文总结 RocketMQ 的存储结构-基础 【RocketMQ 存储】- 一文总结 RocketMQ 的存储结构-基础...

S4 HANA明确税金本币和外币之间转换汇率确定(OBC8)

本文主要介绍在S4 HANA OP中明确明确税金本币和外币之间转换汇率确定(OBC8)相关设置。具体请参照如下内容&#xff1a; 明确税金本币和外币之间转换汇率确定(OBC8) 以上配置&#xff0c;我们可以根据不同公司代码所配置的使用不同的汇率来对税金外币和本币之间进行换算。来针对…...

Cocos Creator 3.8 2D 游戏开发知识点整理

目录 Cocos Creator 3.8 2D 游戏开发知识点整理 1. Cocos Creator 3.8 概述 2. 2D 游戏核心组件 (1) 节点&#xff08;Node&#xff09;与组件&#xff08;Component&#xff09; (2) 渲染组件 (3) UI 组件 3. 动画系统 (1) 传统帧动画 (2) 动画编辑器 (3) Spine 和 …...

梯度提升用于高效的分类与回归

使用 决策树&#xff08;Decision Tree&#xff09; 实现 梯度提升&#xff08;Gradient Boosting&#xff09; 主要是模拟 GBDT&#xff08;Gradient Boosting Decision Trees&#xff09; 的原理&#xff0c;即&#xff1a; 第一棵树拟合原始数据计算残差&#xff08;负梯度…...

【单细胞第二节:单细胞示例数据分析-GSE218208】

GSE218208 1.创建Seurat对象 #untar(“GSE218208_RAW.tar”) rm(list ls()) a data.table::fread("GSM6736629_10x-PBMC-1_ds0.1974_CountMatrix.tsv.gz",data.table F) a[1:4,1:4] library(tidyverse) a$alias:gene str_split(a$alias:gene,":",si…...

设计模式 - 行为模式_Template Method Pattern模板方法模式在数据处理中的应用

文章目录 概述1. 核心思想2. 结构3. 示例代码4. 优点5. 缺点6. 适用场景7. 案例&#xff1a;模板方法模式在数据处理中的应用案例背景UML搭建抽象基类 - 数据处理的 “总指挥”子类定制 - 适配不同供应商供应商 A 的数据处理器供应商 B 的数据处理器 在业务代码中整合运用 8. 总…...

新春登蛇山:告别岁月,启航未来

大年初一&#xff0c;晨曦透过薄雾&#xff0c;温柔地洒在武汉的大街小巷。2025 年的蛇年春节&#xff0c;带着新春的喜气与希望悄然而至。我站在蛇山脚下&#xff0c;心中涌动着复杂的情感&#xff0c;因为今天&#xff0c;我不仅将与家人一起登山揽胜&#xff0c;更将在这一天…...

hive:基本数据类型,关于表和列语法

基本数据类型 Hive 的数据类型分为基本数据类型和复杂数据类型 加粗的是常用数据类型 BOOLEAN出现ture和false外的其他值会变成NULL值 没有number,decimal类似number 如果输入的数据不符合数据类型, 映射时会变成NULL, 但是数据本身并没有被修改 创建表 创建表的本质其实就是在…...

安装最小化的CentOS7后,执行yum命令报错Could not resolve host mirrorlist.centos.org; 未知的错误

文章目录 安装最小化的CentOS7后&#xff0c;执行yum命令报错"Could not resolve host: mirrorlist.centos.org; 未知的错误"错误解决方案&#xff1a; 安装最小化的CentOS7后&#xff0c;执行yum命令报错"Could not resolve host: mirrorlist.centos.org; 未知…...

图论——spfa判负环

负环 图 G G G中存在一个回路&#xff0c;该回路边权之和为负数&#xff0c;称之为负环。 spfa求负环 方法1:统计每个点入队次数, 如果某个点入队n次, 说明存在负环。 证明&#xff1a;一个点入队n次&#xff0c;即被更新了n次。一个点每次被更新时所对应最短路的边数一定是…...

软件工程概论试题三

一、单选 1.需求确认主要检査五个方面的内容&#xff0c;其中那一项是为了保证文档中的需求不互相冲突(即不应该有相互矛盾的约束或者对同一个系统功能有不同的描述)。 A.现实性 B. 可验证性 C.一致性 D.正确性 E.完整性 正答&#xff1a;C 2.下列开发方法中&#xff0c;( )不…...

21.3-启动流程、编码风格(了解) 第21章-FreeRTOS项目实战--基础知识之新建任务、启动流程、编码风格、系统配置 文件组成和编码风格(了解)

21.3-启动流程、编码风格(了解) 启动流程 第一种启动流程(我们就使用这个): 在main函数中将硬件初始化、RTOS系统初始化&#xff0c;同时创建所有任务&#xff0c;再启动RTOS调度器。 第二种启动流程&#xff1a; 在main函数中将硬件初始化、RTOS系统初始化&#xff0c;只…...

未来无线技术的发展方向

未来无线技术的发展趋势呈现出多样化、融合化的特点&#xff0c;涵盖速度、覆盖范围、应用领域、频段利用、安全性等多个方面。这些趋势将深刻改变人们的生活和社会的运行方式。 传输速度提升&#xff1a;Wi-Fi 技术迭代加快&#xff0c;如 Wi-Fi7 理论峰值速率达 46Gbps&#…...

Qt5离线安装包无法下载问题解决办法

想在电脑里装一个Qt&#xff0c;但是直接报错。果然还是有解决办法滴。 qt download from your ip is not allowed Qt5安装包下载办法 方法一&#xff1a;简单直接&#xff0c;直接科学一下&#xff0c;不过违法行为咱不做&#xff0c;遵纪守法好公民&#xff08;不过没办法阻…...

qt-C++笔记之QLine、QRect、QPainterPath、和自定义QGraphicsPathItem、QGraphicsRectItem的区别

qt-C笔记之QLine、QRect、QPainterPath、和自定义QGraphicsPathItem、QGraphicsRectItem的区别 code review! 参考笔记 1.qt-C笔记之重写QGraphicsItem的paint方法(自定义QGraphicsItem) 文章目录 qt-C笔记之QLine、QRect、QPainterPath、和自定义QGraphicsPathItem、QGraphic…...

doris:导入时实现数据转换

Doris 在数据导入时提供了强大的数据转换能力&#xff0c;可以简化部分数据处理流程&#xff0c;减少对额外 ETL 工具的依赖。主要支持以下四种转换方式&#xff1a; 列映射&#xff1a;将源数据列映射到目标表的不同列。 列变换&#xff1a;使用函数和表达式对源数据进行实时…...

KubeSphere 容器平台高可用:环境搭建与可视化操作指南

Linux_k8s篇 欢迎来到Linux的世界&#xff0c;看笔记好好学多敲多打&#xff0c;每个人都是大神&#xff01; 题目&#xff1a;KubeSphere 容器平台高可用&#xff1a;环境搭建与可视化操作指南 版本号: 1.0,0 作者: 老王要学习 日期: 2025.06.05 适用环境: Ubuntu22 文档说…...

Vue3 + Element Plus + TypeScript中el-transfer穿梭框组件使用详解及示例

使用详解 Element Plus 的 el-transfer 组件是一个强大的穿梭框组件&#xff0c;常用于在两个集合之间进行数据转移&#xff0c;如权限分配、数据选择等场景。下面我将详细介绍其用法并提供一个完整示例。 核心特性与用法 基本属性 v-model&#xff1a;绑定右侧列表的值&…...

质量体系的重要

质量体系是为确保产品、服务或过程质量满足规定要求&#xff0c;由相互关联的要素构成的有机整体。其核心内容可归纳为以下五个方面&#xff1a; &#x1f3db;️ 一、组织架构与职责 质量体系明确组织内各部门、岗位的职责与权限&#xff0c;形成层级清晰的管理网络&#xf…...

ardupilot 开发环境eclipse 中import 缺少C++

目录 文章目录 目录摘要1.修复过程摘要 本节主要解决ardupilot 开发环境eclipse 中import 缺少C++,无法导入ardupilot代码,会引起查看不方便的问题。如下图所示 1.修复过程 0.安装ubuntu 软件中自带的eclipse 1.打开eclipse—Help—install new software 2.在 Work with中…...

12.找到字符串中所有字母异位词

&#x1f9e0; 题目解析 题目描述&#xff1a; 给定两个字符串 s 和 p&#xff0c;找出 s 中所有 p 的字母异位词的起始索引。 返回的答案以数组形式表示。 字母异位词定义&#xff1a; 若两个字符串包含的字符种类和出现次数完全相同&#xff0c;顺序无所谓&#xff0c;则互为…...

ios苹果系统,js 滑动屏幕、锚定无效

现象&#xff1a;window.addEventListener监听touch无效&#xff0c;划不动屏幕&#xff0c;但是代码逻辑都有执行到。 scrollIntoView也无效。 原因&#xff1a;这是因为 iOS 的触摸事件处理机制和 touch-action: none 的设置有关。ios有太多得交互动作&#xff0c;从而会影响…...

Unity | AmplifyShaderEditor插件基础(第七集:平面波动shader)

目录 一、&#x1f44b;&#x1f3fb;前言 二、&#x1f608;sinx波动的基本原理 三、&#x1f608;波动起来 1.sinx节点介绍 2.vertexPosition 3.集成Vector3 a.节点Append b.连起来 4.波动起来 a.波动的原理 b.时间节点 c.sinx的处理 四、&#x1f30a;波动优化…...

如何更改默认 Crontab 编辑器 ?

在 Linux 领域中&#xff0c;crontab 是您可能经常遇到的一个术语。这个实用程序在类 unix 操作系统上可用&#xff0c;用于调度在预定义时间和间隔自动执行的任务。这对管理员和高级用户非常有益&#xff0c;允许他们自动执行各种系统任务。 编辑 Crontab 文件通常使用文本编…...

NPOI操作EXCEL文件 ——CAD C# 二次开发

缺点:dll.版本容易加载错误。CAD加载插件时&#xff0c;没有加载所有类库。插件运行过程中用到某个类库&#xff0c;会从CAD的安装目录找&#xff0c;找不到就报错了。 【方案2】让CAD在加载过程中把类库加载到内存 【方案3】是发现缺少了哪个库&#xff0c;就用插件程序加载进…...

CVPR2025重磅突破:AnomalyAny框架实现单样本生成逼真异常数据,破解视觉检测瓶颈!

本文介绍了一种名为AnomalyAny的创新框架&#xff0c;该方法利用Stable Diffusion的强大生成能力&#xff0c;仅需单个正常样本和文本描述&#xff0c;即可生成逼真且多样化的异常样本&#xff0c;有效解决了视觉异常检测中异常样本稀缺的难题&#xff0c;为工业质检、医疗影像…...