安卓(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,一经查实,立即删除!

相关文章

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

目录 一、线程和进程 &#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 …

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

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

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

在当今这个数字化快速发展的时代&#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…

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

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

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|读取图像&…

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

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

基于云计算、大数据与YOLO设计的火灾/火焰目标检测

摘要&#xff1a;本研究针对火灾早期预警检测需求&#xff0c;采用在Kaggle平台获取数据、采用云计算部署的方式&#xff0c;以YOLOv11构建模型&#xff0c;使用云计算服务器训练模型。经训练&#xff0c;box loss从约3.5降至1.0&#xff0c;cls loss从约4.0降至1.0&#xff0c…

计算机毕业设计Python+CNN卷积神经网络考研院校推荐系统 考研分数线预测 考研推荐系统 考研爬虫 考研大数据 Hadoop 大数据毕设 机器学习

温馨提示&#xff1a;文末有 CSDN 平台官方提供的学长联系方式的名片&#xff01; 温馨提示&#xff1a;文末有 CSDN 平台官方提供的学长联系方式的名片&#xff01; 温馨提示&#xff1a;文末有 CSDN 平台官方提供的学长联系方式的名片&#xff01; 作者简介&#xff1a;Java领…

小程序-基础加强-自定义组件

前言 这次讲自定义组件 1. 准备今天要用到的项目 2. 初步创建并使用自定义组件 这样就成功在home中引入了test组件 在json中引用了这个组件才能用这个组件 现在我们来实现全局引用组件 在app.json这样使用就可以了 3. 自定义组件的样式 发现页面里面的文本和组件里面的文…

docker安装emqx

emqx安装 拉取emqx镜像 docker pull emqx/emqx:v4.1.0 运行docker容器 docker run -tid --name emqx -p 1883:1883 -p 8083:8083 -p 8081:8081 -p 8883:8883 -p 8084:8084 -p 18083:18083 emqx/emqx:v4.1.0 放行端口 1、如果要是自己的虚拟机&#xff0c;并且关闭了防火墙&a…

【4Day创客实践入门教程】Day4 迈向高手之路——进一步学习!

Day4 迈向高手之路——进一步学习&#xff01; 目录 Day4 迈向高手之路——进一步学习&#xff01;更多的开发板外壳制作 Day0 创想启程——课程与项目预览Day1 工具箱构建——开发环境的构建Day2 探秘微控制器——单片机与MicroPython初步Day3 实战演练——桌面迷你番茄钟Day4…

深度学习之“缺失数据处理”

缺失值检测 缺失数据就是我们没有的数据。如果数据集是由向量表示的特征组成&#xff0c;那么缺失值可能表现为某些样本的一个或多个特征因为某些原因而没有测量的值。通常情况下&#xff0c;缺失值由特殊的编码方式。如果正常值都是正数&#xff0c;那么缺失值可能被标记为-1…

日志收集Day007

1.配置ES集群TLS认证: (1)elk101节点生成证书文件 cd /usr/share/elasticsearch ./bin/elasticsearch-certutil cert -out config/elastic-certificates.p12 -pass "" --days 3650 (2)elk101节点为证书文件修改属主和属组 chown elasticsearch:elasticsearch con…

arm-linux-gnueabihf安装

Linaro Releases windows下打开wsl2中的ubuntu&#xff0c;资源管理器中输入&#xff1a; \\wsl$gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabihf.tar.xz 复制到/home/ark01/tool 在 Ubuntu 中创建目录&#xff1a; /usr/local/arm&#xff0c;命令如下&#xff1a; …

LabVIEW透镜多参数自动检测系统

在现代制造业中&#xff0c;提升产品质量检测的自动化水平是提高生产效率和准确性的关键。本文介绍了一个基于LabVIEW的透镜多参数自动检测系统&#xff0c;该系统能够在单一工位上完成透镜的多项质量参数检测&#xff0c;并实现透镜的自动搬运与分选&#xff0c;极大地提升了检…

【算法】动态规划专题① ——线性DP python

目录 引入简单实现稍加变形举一反三实战演练总结 引入 楼梯有个台阶&#xff0c;每次可以一步上1阶或2阶。一共有多少种不同的上楼方法&#xff1f; 怎么去思考&#xff1f; 假设就只有1个台阶&#xff0c;走法只有&#xff1a;1 只有2台阶&#xff1a; 11&#xff0c;2 只有3台…