安卓(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 

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

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

相关文章

Java进阶six junit单元测试,反射,注解,动态代理

前言 Java进阶课程的第六篇&#xff0c;也是最后一篇&#xff0c;junit单元测试,反射,注解,动态代理相关内容 包含知识点 junit单元测试 反射 1.内部类Student&#xff1a; 包含私有/公共字段和方法 包含默认构造器和私有构造器 2.获取Class对象的三种方式&#xff1a; .…

程序代码篇---Python随机数

文章目录 前言Python随机数函数random.random()random.randint(a,b)random.randrange(start,stop[,step])random.uniform(a,b)random.choice(sequence)random.sample(population,k)random.shuffle(x[,random])random.seed(aNone) 总结 前言 以上就是今天要讲的内容&#xff0c…

爬虫基础(四)线程 和 进程 及相关知识点

目录 一、线程和进程 &#xff08;1&#xff09;进程 &#xff08;2&#xff09;线程 &#xff08;3&#xff09;区别 二、串行、并发、并行 &#xff08;1&#xff09;串行 &#xff08;2&#xff09;并行 &#xff08;3&#xff09;并发 三、爬虫中的线程和进程 &am…

自签证书的dockerfile中from命令无法拉取镜像而docker的pull命令能拉取镜像

问题现象&#xff1a; docker pull images拉取镜像正常 dockerfile中的from命令拉取镜像就会报出证书错误。报错信息如下&#xff1a; [bjxtbwj-kvm-test-jenkins-6-243 ceshi_dockerfile]$ docker build . [] Building 0.4s (3/3) FINISHED …

半导体SAP管理系统:数字化转型的驱动力

在当今全球科技竞争日益激烈的背景下&#xff0c;半导体行业作为信息技术的基石&#xff0c;其生产效率、质量控制和成本优化直接关系到企业的市场竞争力和可持续发展。随着数字化转型的深入&#xff0c;半导体企业纷纷寻求高效、智能的管理系统以提升运营效率。SAP管理系统&am…

计算机网络 IP 网络层 2 (重置版)

IP的简介&#xff1a; IP 地址是互联网协议地址&#xff08;Internet Protocol Address&#xff09;的简称&#xff0c;是分配给连接到互联网的设备的唯一标识符&#xff0c;用于在网络中定位和通信。 IP编制的历史阶段&#xff1a; 1&#xff0c;分类的IP地址&#xff1a; …

使用Redis生成全局唯一ID示例

全局ID生成器,一种在分布式系统下用来生成全局唯一ID的工具,一般满足一下要求特性 1.唯一性 2.高性能 3.安全性 4.递增性 5.高可用 Component public class RedisIdWorker {/*** 定义一个开始的时间戳(秒级)* param args*/private static final long BEGIN_TIMESTAMP 16…

面对企业文件交换难题,镭速跨网文件交换系统是如何解决的?

在当今这个数字化快速发展的时代&#xff0c;企业越来越依赖于数据交换来维持其业务运作。无论是内部网络之间的沟通还是与外部合作伙伴的数据共享&#xff0c;高效且安全的跨网文件交换都显得尤为重要。然而&#xff0c;在实际操作中&#xff0c;许多企业面临着各种各样的挑战…

Many Whelps! Handle It! (10 player) Many Whelps! Handle It! (25 player)

http://db.nfuwow.com/80/?achievement4403 http://db.nfuwow.com/80/?achievement4406 最少扣你50DKP! 第二阶段 当奥妮克希亚升空后&#xff0c;在10秒内引出50只奥妮克希亚雏龙&#xff0c;随后击败奥妮克希亚。 World of Warcraft [CLASSIC][80猎人][Grandel][最少扣你5…

【Java异步编程】CompletableFuture基础(1):创建不同线程的子任务、子任务链式调用与异常处理

文章目录 1. 三种实现接口2. 链式调用&#xff1a;保证链的顺序性与异步性3. CompletableFuture创建CompletionStage子任务4. 处理异常a. 创建回调钩子b. 调用handle()方法统一处理异常和结果 5. 如何选择线程池&#xff1a;不同的业务选择不同的线程池 CompletableFuture是JDK…

自制虚拟机(C/C++)(一、分析语法和easyx运用,完整虚拟机实现)

网上对虚拟机的解释很多&#xff0c;其实本质就一句话 虚拟机就是机器语言解释器 我们今天要实现汇编语言解释器&#xff0c;下一次再加上ndisasm反汇编器就是真正虚拟机了 注:这里的虚拟机指的是VMware一类的&#xff0c;而不是JVM&#xff0c;python一样的高级语言解释器 …

如何自己设计一个类似 Dubbo 的 RPC 框架?

面试题 如何自己设计一个类似 Dubbo 的 RPC 框架&#xff1f; 面试官心理分析 说实话&#xff0c;就这问题&#xff0c;其实就跟问你如何自己设计一个 MQ 一样的道理&#xff0c;就考两个&#xff1a; 你有没有对某个 rpc 框架原理有非常深入的理解。 你能不能从整体上来思考…

python 使用Whisper模型进行语音翻译

目录 一、Whisper 是什么? 二、Whisper 的基本命令行用法 三、代码实践 四、是否保留Token标记 五、翻译长度问题 六、性能分析 一、Whisper 是什么? Whisper 是由 OpenAI 开源的一个自动语音识别(Automatic Speech Recognition, ASR)系统。它的主要特点是: 多语言…

36. printf

1. printf 格式化函数说的是 printf、 sprintf 和 scanf 这样的函数&#xff0c;分为格式化输入和格式化输出两类函数。学习 C 语言的时候常常通过 printf 函数在屏幕上显示字符串&#xff0c;通过 scanf 函数从键盘获取输入。这样就有了输入和输出了&#xff0c;实现了最基本…

实验八 JSP访问数据库

实验八 JSP访问数据库 目的&#xff1a; 1、熟悉JDBC的数据库访问模式。 2、掌握使用My SQL数据库的使用 实验要求&#xff1a; 1、通过JDBC访问mysql数据&#xff0c;实现增删改查功能的实现 2、要求提交实验报告&#xff0c;将代码和实验结果页面截图放入报告中 实验过程&a…

python学opencv|读取图像(四十六)使用cv2.bitwise_or()函数实现图像按位或运算

【0】基础定义 按位与运算&#xff1a;全1取1&#xff0c;其余取0。按位或运算&#xff1a;全0取0&#xff0c;其余取1。 【1】引言 前序学习进程中&#xff0c;已经对图像按位与计算进行了详细探究&#xff0c;相关文章链接如下&#xff1a; python学opencv|读取图像&…

Flink (十二) :Table API SQL (一) 概览

Apache Flink 有两种关系型 API 来做流批统一处理&#xff1a;Table API 和 SQL。Table API 是用于 Scala 和 Java 语言的查询API&#xff0c;它可以用一种非常直观的方式来组合使用选取、过滤、join 等关系型算子。Flink SQL 是基于 Apache Calcite 来实现的标准 SQL。无论输入…

爬虫基础(六)代理简述

目录 一、什么是代理 二、基本原理 三、代理分类 一、什么是代理 爬虫一般是自动化的&#xff0c;当我们自动运行时 爬虫自动抓取数据&#xff0c;但一会就出现了错误&#xff1a; 如&#xff0c;您的访问频率过高&#xff01; 这是因为网站的反爬措施&#xff0c;如果频…

「 机器人 」利用数据驱动模型替代仿真器:加速策略训练并降低硬件依赖

前言 在强化学习(Reinforcement Learning, RL)中,策略训练需要大量的交互数据(状态、动作、奖励、下一状态),而这些数据通常来自仿真器或真实硬件。传统高保真仿真器虽然能在一定程度上模拟飞行器的动力学,但往往计算量大、开发成本高,且仍可能与真实环境存在差距。为此…

使用vhd虚拟磁盘安装两个win10系统

使用vhd虚拟磁盘安装两个win10系统 前言vhd虚拟磁盘技术简介准备工具开始动手实践1.winX选择磁盘管理2.选择“操作”--“创建VHD”3.自定义一个位置&#xff0c;输入虚拟磁盘大小4.右键初始化磁盘5.选择GPT分区表格式6.右键新建简单卷7.给卷起个名字&#xff0c;用于区分8.打开…