当前位置: 首页 > 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;使用函数和表达式对源数据进行实时…...

新版231普通阿里滑块 自动化和逆向实现 分析

声明: 本文章中所有内容仅供学习交流使用&#xff0c;不用于其他任何目的&#xff0c;抓包内容、敏感网址、数据接口等均已做脱敏处理&#xff0c;严禁用于商业用途和非法用途&#xff0c;否则由此产生的一切后果均与作者无关&#xff01; 逆向过程 补环境逆向 部分补环境 …...

如何构建树状的思维棱镜认知框架

在思维与知识管理中&#xff0c;“树状思维棱镜”通常指一种层级式、可多维度展开和不断深入&#xff08;下钻&#xff09;的认知框架。它不仅仅是普通的树状结构&#xff08;如传统思维导图&#xff09;&#xff0c;更强调“棱镜”所体现的多视角、多维度切换与综合分析的能力…...

openRv1126 AI算法部署实战之——ONNX模型部署实战

在RV1126开发板上部署ONNX算法&#xff0c;实时目标检测RTSP传输。视频演示地址 rv1126 yolov5 实时目标检测 rtsp传输_哔哩哔哩_bilibili 一、准备工作 1.从官网下载YOLOv5-v7.0工程&#xff08;YOLOv5的第7个版本&#xff09; 手动在线下载&#xff1a; Releases ultraly…...

Vue 组件开发:构建高效可复用的前端界面要素

1 引言 在现代 Web 开发中,构建高效且可复用的前端界面要素是提升开发效率和用户体验的关键。Vue.js 作为一种轻量级且功能强大的前端框架,提供了丰富的工具和机制,帮助开发者快速构建高质量的应用程序。通过合理设计和封装 Vue 组件,我们可以实现组件的高效复用,提高开发…...

Vue.js组件开发-实现全屏平滑移动、自适应图片全屏滑动切换

使用Vue实现全屏平滑移动、自适应图片全屏滑动切换的功能。使用Vue 3和Vue Router&#xff0c;并结合一些CSS样式来完成这个效果。 步骤 创建Vue项目&#xff1a;使用Vue CLI创建一个新的Vue项目。准备图片&#xff1a;将需要展示的图片放在项目的public目录下。创建组件&…...

水果实体店品牌数字化:RWA + 智能体落地方案

一、方案背景 随着数字化技术的迅猛发展&#xff0c;实体零售行业正面临前所未有的挑战与机遇。传统的零售模式难以满足消费者对个性化、便捷化、智能化的需求&#xff0c;尤其是在水果等生鲜商品领域&#xff0c;如何通过技术手段提升运营效率、增强顾客体验、拓宽盈利模式&a…...

DeepSeek模型:开启人工智能的新篇章

DeepSeek模型&#xff1a;开启人工智能的新篇章 在当今快速发展的技术浪潮中&#xff0c;人工智能&#xff08;AI&#xff09;已经成为了推动社会进步和创新的核心力量之一。而DeepSeek模型&#xff0c;作为AI领域的一颗璀璨明珠&#xff0c;正以其强大的功能和灵活的用法&…...

Kubernetes 环境中的自动化运维实战指南

Kubernetes 作为容器编排领域的领导者,已经成为云原生应用的核心基础设施。然而,随着集群规模的扩大和应用的复杂化,手动运维 Kubernetes 集群变得愈发困难。自动化运维成为提升效率、保障系统稳定性的关键。本文将详细介绍如何在 Kubernetes 环境中实施自动化运维,涵盖工具…...

深入解析 C++17 中的 std::not_fn

文章目录 1. std::not_fn 的定义与目的2. 基本用法2.1 基本示例2.2 使用 Lambda 表达式2.3 与其他函数适配器的比较3. 在标准库中的应用3.1 结合标准库算法使用3.1.1 std::find_if 中的应用3.1.2 std::remove_if 中的应用3.1.3 其他标准库算法中的应用4. 高级技巧与最佳实践4.1…...

unity实现回旋镖函数

最近学习unity2D&#xff0c;想实现一个回旋镖武器&#xff0c;发出后就可以在角色周围回旋。 一、目标 1.不是一次性的&#xff0c;扔出去、返回、没有了&#xff1b;而是扔出去&#xff0c;返回到角色后方相同距离&#xff0c;再次返回&#xff1b;再次返回&#xff0c;永远…...