首页 > 其他分享 >Android实战:实现注册界面

Android实战:实现注册界面

时间:2024-08-20 18:23:41浏览次数:8  
标签:实战 Toast 界面 样式 import Android 文本 id android

目录

前言

布局图

实现过程

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"

 

 

<iframe allowfullscreen="true" data-mediaembed="csdn" frameborder="0" id="ZZCHimPB-1724149560328" src="https://live.csdn.net/v/embed/418835"></iframe>

android实现的注册界面


以上就是本篇所有内容~

若有不足,欢迎指正~ 

标签:实战,Toast,界面,样式,import,Android,文本,id,android
From: https://blog.csdn.net/zhyhgx/article/details/141359843

相关文章

  • Android T don't abort background activity starts
    log:2024-08-2015:45:12.457581-1128ActivityTaskManagersystem_processISTARTu0{act=android.intent.action.MAINcat=[android.intent.category.LAUNCHER]flg=0x10000000pkg=acr.browser.lightningcmp=acr.browser.lightning/.Ma......
  • MySQL-MGR实战指南:打造企业级高可用数据库集群
    文章目录前言MGR的介绍事务处理流程:实验测试环境:结束语前言在数字化时代,企业的数据安全和业务连续性至关重要。想象一下,当关键业务数据存储在数据库中,而数据库突然出现故障,或者面临硬件故障、网络中断、自然灾害等不可预知的灾难性事件时,企业如何确保数据的完整性和......
  • Swift与UIKit:构建卓越用户界面的艺术
    标题:Swift与UIKit:构建卓越用户界面的艺术在iOS应用开发的世界中,UIKit是构建用户界面的基石。自从Swift语言问世以来,它与UIKit的结合就为开发者提供了一个强大而直观的工具集,用于创建直观、响应迅速的应用程序。本文将带领读者深入了解如何使用Swift和UIKit来构建引人入胜的......
  • LLM应用实战: 产业治理多标签分类
    1. 背景许久未见,甚是想念~近期本qiang~换了工作,处于新业务适应期,因此文章有一段时间未更新,理解万岁!现在正在着手的工作是产业治理方面,主要负责其中一个功能模块,即按照产业治理标准体系,针对企业介绍及其专利数据进行多标签分类。本期的干货就是分享关于如何基于LLM实现数量多......
  • Android 11.0 通过系统属性适配多种分辨率开机动画功能实现
    1.前言 在11.0的系统ROM定制化开发中,在关于开机动画这部分由于产品需要适配多种分辨率,所以就需要在出rom固件的时候,就需要根据系统属性来适配显示哪种分辨率开机动画,所以接下来就来看怎么设置系统属性,然后在开机动画阶段怎么实现这个功能2.通过系统属性适配多种分辨率开......
  • Android CDD(兼容性定义文档)
    1.什么是AndroidCDDAndroid兼容性定义文档(CDD)列举了设备需要满足哪些要求才能与最新的Android版本兼容。也就是说每当Android新版本更新时,我们需要检查Android最新的CDD,保证我们的系统满足AndroidCDD的要求。设备实现必须满足此兼容性定义文档(包括以参考资料的形式纳入......
  • B2B进销存ERP后台管理系统的逻辑架构与设计,AxureRP原型产品经理实战案例
    模块分析:进销存系统是一种用于企业管理库存、销售和采购活动的信息系统。它的主要作用包括但不限于以下几个方面:1.库存管理实时库存跟踪:准确记录每种商品的库存数量,确保数据的实时性和准确性。库存预警:当库存量低于预设的安全水平时自动发出警报,防止缺货或积压。先进先出(......
  • Android逆向题解-攻防世界-Ph0en1x-100
    jeb反编译apk主要代码是if那个判断,getFlag取字符串用getSecret加密,和输入字符串encrypt加密后再getSecret加密,进行比较,两边同样都是getSecret加密,那比较可以简化成this.getFlag()==this.encrypt(s)。也就是输入字符经过encrypt加密后等于getFlag的字符串即可。protec......
  • Android about event log
    EventLogTags.logtags,它是个日志工具,很多模块有该文件.eventlog在framework层常常使用,通过类似EventLog.writeEvent()写log,这种log被保存在/system/etc/event-log-tags../frameworks/base/services/core/java/com/android/server/wm/EventLogTags.logtagswm_set_resumed_......
  • JSP学校新生入学管理系统设计与实现4om82(程序+源码+数据库+调试部署+开发环境)系统界面
    系统程序文件列表项目功能:学生,新生信息,类型开题报告内容JSP学校新生入学管理系统设计与实现开题报告一、研究背景与意义1.1研究背景随着信息技术的飞速发展,高校信息化管理水平不断提高。传统的学校新生入学管理模式存在诸多弊端,如人工操作效率低、数据查找困难、资......