Android实战:实现注册界面

news2024/9/22 11:30:03

目录

前言

布局图

实现过程

1.导入图片

2.创建分割线样式

3.创建文本样式

4.创建输入框样式

5.XML布局文件

测试

6.实现注册功能


前言

在前面,我们已经学习了一些常见的界面布局和界面控件,那么本篇我们就来结合前面所学的知识,来实现一个注册界面。

布局图

我们最终要实现的界面:

那么上面这样的布局是怎么实现的呢?

实现过程

1.导入图片

这里我们需要用到三张图片,分别命名为qq_icon.png、wechat.png、email.png

我们可以直接去iconfont-阿里巴巴矢量图标库 进行查找相应的图片。

2.创建分割线样式

我们可以看到上面的图片中,有7条白色横杠和1条白色的竖杠,那这个是怎么实现的?我们可以在res/values/styles.xml文件下创建一个hLine的样式和一个vLine的样式。

代码如下:

<resources>
    <!-- 定义水平分割线的样式 -->
    <style name="hLine">
        <item name="android:layout_width">match_parent</item> <!-- 宽度匹配父容器 -->
        <item name="android:layout_height">1dp</item> <!-- 高度为1dp,实现细线效果 -->
        <item name="android:background">@color/white</item> <!-- 背景颜色为白色,形成白色分割线 -->
    </style>
    <!-- 定义垂直分割线的样式 -->
    <style name="vLine">
        <item name="android:layout_width">1dp</item> <!-- 宽度为1dp,实现细线效果 -->
        <item name="android:layout_height">match_parent</item> <!-- 高度匹配父容器 -->
        <item name="android:background">@color/white</item> <!-- 背景颜色为白色,形成白色分割线 -->
    </style>
</resources>

3.创建文本样式

在布局图中,我们可以看到,在注册界面中,需要用到“QQ注册”和“微信注册”这两个文本信息,这两个文本具有相同的样式,所以我们可以把这些相同的属性给提取出来。同理的,我们可以看到后面的“名字”、“邮箱”、“密码”、“性别”、“兴趣”这些文本的信息,也是具有同样的样式,所以我们也可以看这些相同的属性给提取出来。

所以,我们针对QQ注册和微信注册这两个文本信息,我们可以创建一个tvOne的样式来存放。样式要放在resources中。

<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:paddingTop">45dp</item>                     --文本距离顶部距离
    <item name="android:drawablePadding">8dp</item>             --文本距离顶部距离
    <item name="android:gravity">center_horizontal</item>       --文本位置
    <item name="android:textSize">20sp</item>                              --文本大小
    <item name="android:textColor">@color/white</item>          --文本颜色
</style>

那么对于名字等的信息,我们可以在styles.xml中创建一个tvTwo样式存放。

-- 定义一个名为"tvTwo"的样式,主要用于设置文本视图(如TextView)的布局和视觉属性 --

<style name="tvTwo">
    <!-- 设置组件的布局宽度为包裹内容,即根据内容自动调整其宽度 -->
    <item name="android:layout_width">wrap_content</item>
    <!-- 设置组件的布局高度为包裹内容,即根据内容自动调整其高度 -->
    <item name="android:layout_height">wrap_content</item>
    <!-- 设置文本大小为20sp(scaled pixel),sp是一种根据设备密度自动调整的单位,以确保文本的可读性 -->
    <item name="android:textSize">20sp</item>
    <!-- 设置文本颜色为白色 -->
    <item name="android:textColor">@color/white</item>
    <!-- 设置组件背景为null,即不显示背景 -->
    <item name="android:background">@null</item>
    <!-- 设置组件左边距为20sp,用于分隔组件与左边界的距离 -->
    <item name="android:layout_marginLeft">20sp</item>
</style>

4.创建输入框样式

在注册界面需要一些用户信息,如名字、邮箱、密码等,这些输入框具有相同的样式,所以我们可以将具有相同属性的代码提取出来,放到一个样式etOne中。

    <!-- 定义一个名为etOne的样式,主要用于设置EditText组件的布局和样式属性 -->
    <style name="etOne">
        <!-- 设置布局宽度为匹配父容器,使EditText组件的宽度与其父布局的宽度一致 -->
        <item name="android:layout_width">match_parent</item>
        <!-- 设置布局高度为包裹内容,使EditText组件的高度根据其内容自动调整 -->
        <item name="android:layout_height">wrap_content</item>
        <!-- 设置左边距为30dp,用于在布局中创建统一的左边空白区域,以区分其他元素 -->
        <item name="android:layout_marginLeft">30dp</item>
        <!-- 设置背景为null,表示EditText组件不显示背景,以突出文本内容 -->
        <item name="android:background">@null</item>
        <!-- 设置文本颜色为白色,确保在任何背景色下都能清晰显示文本内容 -->
        <item name="android:textColor">@color/white</item>
    </style>

5.XML布局文件

我们可以创建一个名为edits.xml的布局,借助上面的样式,就可以实现我们的布局图。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#9EBA78"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="#62764A"
            android:gravity="center_horizontal"
            android:text="注册"
            android:textSize="30dp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="130dp"
            android:orientation="horizontal">

            <TextView
                style="@style/tvOne"
                android:drawableTop="@drawable/qq_icon"
                android:text="QQ注册" />

            <View style="@style/vLine"></View>

            <TextView
                style="@style/tvOne"
                android:drawableTop="@drawable/wechat"
                android:text="微信注册" />
        </LinearLayout>

        <View style="@style/hLine"></View>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="15sp">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:src="@drawable/email" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="使用电子邮箱注册"
                android:textSize="20sp" />
        </LinearLayout>

        <View style="@style/hLine"></View>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="15sp">

            <TextView
                android:id="@+id/name"
                style="@style/tvTwo"
                android:text="姓名" />

            <EditText
                style="@style/etOne"
                android:hint="请输入姓名" />
        </LinearLayout>

        <View style="@style/hLine"></View>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="15sp">

            <TextView
                android:id="@+id/email"
                style="@style/tvTwo"
                android:text="邮箱" />

            <EditText
                style="@style/etOne"
                android:hint="请输入邮箱" />
        </LinearLayout>

        <View style="@style/hLine"></View>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="15sp">

            <TextView
                android:id="@+id/password"
                style="@style/tvTwo"
                android:text="密码" />

            <EditText
                style="@style/etOne"
                android:hint="请输入密码" />
        </LinearLayout>

        <View style="@style/hLine"></View>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="15sp">

            <TextView
                style="@style/tvTwo"
                android:text="性别" />

            <RadioGroup
                android:id="@+id/sex"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginLeft="30dp"
                android:orientation="horizontal">

                <RadioButton
                    android:id="@+id/male"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="男"
                    android:textSize="20sp" />

                <RadioButton
                    android:id="@+id/female"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="20sp"
                    android:text="女"
                    android:textSize="20sp" />
            </RadioGroup>
        </LinearLayout>

        <View style="@style/hLine"></View>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="15sp">

            <TextView
                style="@style/tvTwo"
                android:text="兴趣爱好" />

            <CheckBox
                android:id="@+id/basketball"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="篮球"
                android:textSize="15sp" />

            <CheckBox
                android:id="@+id/shuttle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="羽毛球"
                android:textSize="15sp" />

            <CheckBox
                android:id="@+id/football"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="足球"
                android:textSize="15sp" />

            <CheckBox
                android:id="@+id/tennis"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="网球"
                android:textSize="15sp" />
        </LinearLayout>

        <View style="@style/hLine"></View>

        <View
            android: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" />

        <Button
            android:id="@+id/btn_submit"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_alignParentBottom="true"
            android:background="#728373"
            android:text="提交"
            android:textColor="@color/white"
            android:textSize="18dp" />

    </LinearLayout>
</RelativeLayout>

测试

我们可以创建一个register.java,在里面来实现我们的注册功能。

先通过来设置register的显示界面,我们需要去AndroidManifest.xml注册一下register.java。

运行一下

6.实现注册功能

我们可以在register.java中定义一个Init()方法来获取界面控件并且设置点击事件,再创建一个getData()方法来获取界面上输入的信息,实现OnClickListener和OnCheckedChangeListener接口,并重写onClick()方法(用来处理提交的点击事件)onCheckedChanged()方法(用来处理复选框的点击事件)onCheckedChanged()方法(用来处理单选框的点击事件)

package com.example.newapptext1;

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.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class register extends AppCompatActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener, RadioGroup.OnCheckedChangeListener {

    private EditText et_name,et_password,et_email;
    private RadioGroup radioGroup;
    private CheckBox ball,shuttle,tennis,football;
    private String email,password,name,sex,hobbys;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edits);
        Init();
    }

    /**
     * 初始化用户界面组件
     * 通过ID获取所有必要的界面元素,以便在表单中使用
     */
    private void Init() {
        //获取界面组件
        et_name = findViewById(R.id.ed_name);
        et_password = findViewById(R.id.ed_password);
        et_email = findViewById(R.id.ed_email);

        //获取单选组件
        radioGroup = findViewById(R.id.sex);

        //获取复选框组件
        ball = findViewById(R.id.basketball);
        shuttle = findViewById(R.id.shuttle);
        tennis = findViewById(R.id.tennis);
        football = findViewById(R.id.football);

        Button submit=findViewById(R.id.btn_submit);//提交按钮
        submit.setOnClickListener(this);//提交按钮点击事件

        //设置复合按钮的点击事件
        ball.setOnCheckedChangeListener(this);
        shuttle.setOnCheckedChangeListener(this);
        tennis.setOnCheckedChangeListener(this);
        football.setOnCheckedChangeListener(this);
        hobbys =new String();
        //设置单选组件的点击事件
        radioGroup.setOnCheckedChangeListener(this);
    }

    private void getData(){
        name = et_name.getText().toString();
        password = et_password.getText().toString();
        email = et_email.getText().toString();
    }

    //提交按钮的点击事件
    @Override
    public void onClick(View view) {
        if(view.getId()==R.id.btn_submit){
            getData();
            if(TextUtils.isEmpty(name)){
                Toast.makeText(this,"请输入名字",Toast.LENGTH_SHORT).show();
            }else if(TextUtils.isEmpty(password)){
                Toast.makeText(this,"请输入密码",Toast.LENGTH_SHORT).show();
            }else if(TextUtils.isEmpty(email)){
                Toast.makeText(this,"请输入邮箱",Toast.LENGTH_SHORT).show();
            }else if(TextUtils.isEmpty(sex)){
                Toast.makeText(this,"请选择性别",Toast.LENGTH_SHORT).show();
            }else if (TextUtils.isEmpty(hobbys)){
                Toast.makeText(this,"请选择爱好",Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(this,"注册成功",Toast.LENGTH_SHORT).show();
                Log.i("register","注册信息为:"+"  名字:"+name+"  邮箱:"+email+"  密码:"+password+"  性别:"+sex+"  爱好:"+hobbys);
            }
        }
    }

    // 复合按钮的点击事件
    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        String motion = compoundButton.getText().toString(); // 获取复选框的文本
        if (b) {
            // 如果复选框被选中
            if (!hobbys.contains(motion)) {
                // 如果爱好列表中不包含当前文本
                hobbys = hobbys + motion;
                // 将当前文本添加到爱好列表中
            }
        } else {
            // 如果复选框被取消选中
            if (hobbys.contains(motion)) {
                // 如果爱好列表中包含当前文本
                hobbys = hobbys.replace(motion, "");
                // 从爱好列表中移除当前文本
            }
        }
    }


    //单选组件的点击事件
    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        //根据点击的单选按钮ID更新性别变量
        if(i==R.id.male){
            sex ="男";
        } else if(i==R.id.female){
            sex ="女";
        }
    }

}

我们运行进行测试。

如果我们想对密码保密,那么就可以在输入密码的复合控件中加入

                android:inputType="textPassword"

 

 

android实现的注册界面


以上就是本篇所有内容~

若有不足,欢迎指正~ 

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

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

相关文章

导入导出常见的空指针异常NULLPointException

使用row.getCell()方法去判断是否为空 若不为空再获取 不确定excel文件里的单元格类型 可以使用row.getCell().getCellTypeEnum() 去判断是什么类型 若要把从数据库查询出来的数据导出到excel表格中 要先判断内容是否为空

Spring Boot + MyBatis-Plus 实现 MySQL 主从复制动态数据源切换

Spring Boot MyBatis-Plus 实现 MySQL 主从复制动态数据源切换 一、前言1. 添加依赖2. 配置主从数据源3. 创建数据源配置类4. 创建数据源上下文5. 定义数据源类型6. 配置数据源切换7. 创建DynamicDataSourceConfig8. 创建DynamicRoutingDataSource9. 创建注解10. 使用注解 一、…

1.Java:集合

集合作用&#xff1a; 1.动态保存任意多个对象。 2.提供操作对象方法比如add,remove,set,get等方法。 3.使用集合添加&#xff0c;删除代码简洁。 集合分类 集合分为单列集合以及双列集合。 单列集合&#xff1a; 双列集合&#xff1a; Collection接口特点 1.Collection…

【Nginx】 Nginx Rewrite 相关功能

Nginx服务器利用 ngx_http_rewrite_module 模块解析和处理rewrite请求 此功能依靠 PCRE(perl compatible regular expression)&#xff0c;因此编译之前要安装PCRE库 rewrite是nginx服务器的重要功能之一&#xff0c;用于实现URL的重写&#xff0c;URL的重写是非常有用的功能 比…

智慧环卫与智慧城市:以人文本的创新发展之路

智慧环卫与智慧城市&#xff1a;以人文本的创新发展之路 前言智慧环卫与智慧城市 前言 智慧环卫和智慧城市的建设是当今社会发展的重要趋势&#xff0c;它们对于提升城市管理水平、改善居民生活质量具有至关重要的意义。随着国家政策对新型城市建设的大力推动&#xff0c;智慧…

学习 node.js 六 Markdown 转为 html,zlib

目录 Markdown 转为 html 安装 ejs语法 标签含义 1. 纯文本标签 2. 输出经过 HTML 转义的内容 3. 输出非转义的内容(原始内容) marked browserSync zlib gzip deflate gzip 和 deflate 区别 http请求压缩 Markdown 转为 html 什么是markdown&#xff1f; Markdo…

数据库系列之GaussDB数据库高可用部署方案

GaussDB数据库主备架构的基本组件,以及基于华为云底座和轻量化部署TPOPS两种方式的典型高可用部署场景介绍。 1、GaussDB数据库组件 1.1 GaussDB数据库集中式主备集群基本组件 CM由CM Agent、CM Server和OM monitor构成: CM Agent:管理服务组件,由OMM拉起(周期1秒),主要…

ARM32开发——PWM高级定时器

&#x1f3ac; 秋野酱&#xff1a;《个人主页》 &#x1f525; 个人专栏:《Java专栏》《Python专栏》 ⛺️心若有所向往,何惧道阻且长 文章目录 需求高级定时器通道互补输出开发流程通道配置 打开互补保护电路完整代码 需求 点亮2个灯&#xff0c;采用互补pwm的方式 高级定时…

【Unity脚本】使用脚本修改游戏对象静态属性

【知识链】Unity -> -> 脚本系统 -> 访问游戏对象 -> 静态属性【摘要】本文介绍了Unity中游戏对象的静态和动态类型&#xff0c;并说明了如何修改静态属性。 文章目录 第一章 Unity中的静与动第二章 静态和动态对象1. 静态对象&#xff08;Static Objects&#xf…

Web服务器——————nginx篇

一.What is Web服务器 Web服务器介绍 Web服务器&#xff08;Web Server&#xff09;是指驻留于因特网上某种类型计算机的程序&#xff0c;该程序可以向Web浏览器&#xff08;如Chrome、Firefox、Safari等&#xff09;等客户端提供文档&#xff0c;也可以放置网站文件&#…

4-1-3 arduino驱动直流电机(电机专项教程)

4-1-3 arduino驱动直流电机&#xff08;电机专项教程&#xff09; 4-1-3 arduino驱动直流电机XY-2.5AD电机控制模块家用直流电源改装成项目制作电源示例程序效果演示 4-1-3 arduino驱动直流电机 Arduino控制直流有刷电机的话&#xff0c;通过H桥电路实现转向控制&#xff0c;以…

C语言——预处理

C语言编译步骤 预处理 编译 汇编 链接 预处理 概念&#xff1a; 预处理就是在源文件&#xff08;如.c文件&#xff09;编译之前&#xff0c;所进行的一部分预备操作&#xff0c;这部分操作是由预处理程序自动来完成;当源文件在编译时&#xff0c;编译器会自动调用预处理程序来…

ESP32神经网络初步使用

摘要 本文档描述了如何使用Python和TensorFlow训练一个简单的神经网络模型来预测正弦函数&#xff0c;并将其部署到ESP32微控制器上。 参考文章 使用Python和Arduino在ESP32上预测正弦函数 - Dapenson - 博客园 (cnblogs.com) 最简单体验TinyML、TensorFlow Lite——ESP32跑…

Android Studio修改默认.m2与Gradle user home缓存位置

Android Studio修改默认.m2与Gradle user home缓存位置 1、修改Gradle user home的方法&#xff1a; android studio配置默认.gradle路径_android studio gradle在哪-CSDN博客文章浏览阅读2k次。当android studio新建一个项目时候&#xff0c;默认的.gradle路径均认为是在c盘的…

若依搭建实践

若依要求版本 JDK>1.8 MySQL>5.7 Maven>3.0 Node >12 Redis >3 一、环境下载及安装 我本地JDK1.8 MySQL9.0.1 Maven3.5.4 Node 20.12.2 Redis 5.0.14.1 在若依官网下载需要的版本&#xff0c;目前若依支持版本有四个&#xff0c;我们根据需要选择对应的版本…

集合及数据结构第五节————ArrayList的介绍和应用

系列文章目录 集合及数据结构第五节————ArrayList的介绍和应用 ArrayList的介绍和应用 什么是ArrayLisArrayList使用简单的洗牌算法杨辉三角 文章目录 系列文章目录集合及数据结构第五节————ArrayList的介绍和应用 ArrayList的介绍和应用 一、ArrayList1.什么是Arra…

鸿蒙 使用 expandSafeArea 实现顶部沉浸式导航

1&#xff0c; 先看效果&#xff1b; // 设置顶部绘制延伸到状态栏.expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.TOP]) 2&#xff0c; 直接cv 粘贴代码 // xxx.ets Entry Component struct Example {build() {Column() {Row() {Text().fontSize(40).textAlign(Text…

2_foc转速环_电磁转矩方程与运动方程的传递函数

转速环里面包含有电流环在内&#xff0c;当外面给定转速时&#xff0c;系统通过控制iq来控制电磁转矩&#xff0c;从而电机开始转动。电机在转动的过程&#xff0c;传感器检测到机械角度会改变&#xff0c;也就是说电角度也会改变&#xff0c;由电角度在单位时间的变化&#xf…

【C++ 第十五章】map 和 set 的封装(封装红黑树)

1. map 和 set 的介绍 ⭐map 与 set 分别是STL中的两种序列式容器; 它们是一种树形数据结构的容器&#xff0c;且其的底层构造为一棵红黑树; 而在上一篇文章中提到,其实红黑树本身就是一棵二叉搜索树,是基于二叉搜索树的性质对其增加了平衡的属性来提高其综合性能 ⭐当然也…

ip地址冲突的原因及其解决方法是什么

在当今的信息化时代&#xff0c;网络已成为我们生活和工作中不可或缺的一部分。然而&#xff0c;随着网络设备数量的不断增加&#xff0c;网络管理中的问题也日益凸显&#xff0c;其中IP地址冲突便是常见问题之一。IP地址冲突不仅会导致网络通信不稳定&#xff0c;甚至可能使设…