• 【Android Studio学习】第一篇、制作一个拥有登录和注册功能的简易APP


    目录

    第一部分、前言

    1、目标效果

    2、准备知识

    第二部分、详细步骤

    1、新建Empty工程

    ​2、添加资源文件

    3、搭建注册界面

    4、搭建登录界面

     5、编写注册界面和登录界面的代码

    6、设置APP初始界面

    7、连接手机,编译工程

    第三部分、总结

    1、参考资料

    2、完整工程和代码


    第一部分、前言

            这段时间由于导师的任务安排,我需要做一个简易的APP,来接收单片机上传的数据,并画出数据的波形。因为之前搞过那种特别简单的APP(就新建一个工程就得到的那种APP),这次为了让APP看起来还算是个APP,所以这段时间就学了一下APP的登录和注册APP的底部导航栏的实现及应用APP画简易的波形图等。为了防止自己忘记,这次做了一些简易的笔记并将其分享出来,希望能够给你带来一点小小的灵感。

            那么这一篇文章先讲一下,小白如何从零做一个拥有登录和注册功能的APP

    1、目标效果

            讲了那么多,首先来看一下这篇文章实现的功能视频是怎样的吧!

    2、准备知识

            首先,你的电脑需要装上Android Studio这个软件,并且能够正常工作,关于软件的下载和安装可以百度,太多了,不做介绍。

            其次,目前我电脑上装的版本是Android Studio 3.5.2这还是2019版,已经有点老了,和目前最新的版本还是有点差距,所以小伙伴如果之前一点都没接触过这个软件,那么我还是建议你装最新版本的Android Studio,因为B站的大部分教学视频都是基于最新版本的。

            最后,对于这个软件完全陌生的小白,你先按照博客的方法一步一步来,如果按照我的方式实现不了效果的,我这里推荐两个干货入门视频,第一个是正哥的视频,第二个是我这篇文章参考的视频。(注意:如果按照博客的步骤往下走,各种文件名称尽量保持一致,这样不容易出错

     第一个、【7天Java0基础速成安卓开发】Day1 Android工程代码是怎么运行的_哔哩哔哩_bilibili

    第二个、1 as布局介绍和重要文件说明_哔哩哔哩_bilibili

    第二部分、详细步骤

    1、新建Empty工程

    第一步、新建一个Empty Activity工程

     第二步、新建完成后的界面

    ​2、添加资源文件

    第一步、去values文件下的colors.xml文件添加这行代码

    1. <color name="colorBlack">#000000color>
    2. <color name="colorBlue">#78BDF1color>

    第二步、保存下面这个图标,并将其添加到工程当中,命名为box

    第三步、添加Drawable Resource文件,作为登录框的背景

    命名为user_background

    将文件内部代码,替换成以下代码

    1. "1.0" encoding="utf-8"?>
    2. <shape xmlns:android="http://schemas.android.com/apk/res/android">
    3. <stroke
    4. android:width="1dp"
    5. android:color="@color/colorBlack"
    6. android:background="#CCCCCC"/>
    7. <corners
    8. android:radius="100dp"/>
    9. shape>

     得到最终的效果图

    3、搭建注册界面

    第一步、在com.example.myapplication文件夹下,新建一个Empty Activity

     第二步、将新的Activity命名为:LoginActivity,点击确认

     第三步、重复上述的步骤新建一个Activity,命名为RegisterActivity

     第四步、编写注册界面

     第五步、更改原始代码如下,目的是改为线性布局,且布局的排序方式为垂直

     第六步、注册界面布局代码

    1. "1.0" encoding="utf-8"?>
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. xmlns:app="http://schemas.android.com/apk/res-auto"
    4. xmlns:tools="http://schemas.android.com/tools"
    5. android:layout_width="match_parent"
    6. android:layout_height="match_parent"
    7. android:orientation="vertical"
    8. tools:context=".RegisterActivity">
    9. <LinearLayout
    10. android:layout_width="match_parent"
    11. android:layout_height="wrap_content">
    12. <TextView
    13. android:layout_width="match_parent"
    14. android:layout_height="match_parent"
    15. android:text="欢迎注册账号"
    16. android:textSize="35sp"
    17. android:gravity="center"
    18. android:textColor="@color/colorBlack"
    19. android:layout_marginTop="50dp"
    20. >TextView>
    21. LinearLayout>
    22. <LinearLayout
    23. android:layout_width="match_parent"
    24. android:layout_height="wrap_content"
    25. android:layout_marginTop="20dp"
    26. android:layout_marginLeft="10dp"
    27. android:layout_marginRight="10dp"
    28. >
    29. <TextView
    30. android:layout_width="wrap_content"
    31. android:layout_height="wrap_content"
    32. android:layout_gravity="center"
    33. android:text="请输入用户名称:"
    34. android:layout_marginLeft="10dp"
    35. android:textSize="20sp"
    36. >TextView>
    37. <EditText
    38. android:id="@+id/zhuce_user_name"
    39. android:layout_width="match_parent"
    40. android:layout_height="60dp"
    41. android:layout_marginRight="30dp"
    42. android:textColor="@color/colorBlack"
    43. android:maxLines="1"
    44. >EditText>
    45. LinearLayout>
    46. <LinearLayout
    47. android:layout_width="match_parent"
    48. android:layout_height="wrap_content"
    49. android:layout_marginTop="20dp"
    50. android:layout_marginLeft="10dp"
    51. android:layout_marginRight="10dp"
    52. >
    53. <TextView
    54. android:layout_width="wrap_content"
    55. android:layout_height="wrap_content"
    56. android:layout_gravity="center"
    57. android:text="请输入账户密码:"
    58. android:layout_marginLeft="10dp"
    59. android:textSize="20sp"
    60. >TextView>
    61. <EditText
    62. android:id="@+id/zhuce_user_password"
    63. android:layout_width="match_parent"
    64. android:layout_height="60dp"
    65. android:layout_marginRight="30dp"
    66. android:textColor="@color/colorBlack"
    67. android:maxLines="1"
    68. android:inputType="textPassword"
    69. >EditText>
    70. LinearLayout>
    71. <LinearLayout
    72. android:layout_width="match_parent"
    73. android:layout_height="wrap_content"
    74. android:layout_marginTop="20dp"
    75. android:layout_marginLeft="10dp"
    76. android:layout_marginRight="10dp"
    77. >
    78. <TextView
    79. android:layout_width="wrap_content"
    80. android:layout_height="wrap_content"
    81. android:layout_gravity="center"
    82. android:text="请再次输入密码:"
    83. android:layout_marginLeft="10dp"
    84. android:textSize="20sp"
    85. >TextView>
    86. <EditText
    87. android:id="@+id/zhuce_user_password_again"
    88. android:layout_width="match_parent"
    89. android:layout_height="60dp"
    90. android:layout_marginRight="30dp"
    91. android:textColor="@color/colorBlack"
    92. android:maxLines="1"
    93. android:inputType="textPassword"
    94. >EditText>
    95. LinearLayout>
    96. <LinearLayout
    97. android:layout_width="wrap_content"
    98. android:layout_height="wrap_content"
    99. android:layout_marginTop="40dp"
    100. android:layout_gravity="center">
    101. <Button
    102. android:id="@+id/zhuce_success"
    103. android:layout_width="200dp"
    104. android:layout_height="wrap_content"
    105. android:text="注册完成"
    106. android:textSize="25dp"
    107. android:textColor="@color/colorBlack"
    108. android:background="@color/colorBlue"
    109. >Button>
    110. LinearLayout>
    111. LinearLayout>

     第七步、最终效果

    4、搭建登录界面

     第一步、登录界面的布局编写方式也和上面一样,复制下方代码

      第二步、登录界面布局代码

    1. "1.0" encoding="utf-8"?>
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. xmlns:app="http://schemas.android.com/apk/res-auto"
    4. xmlns:tools="http://schemas.android.com/tools"
    5. android:layout_width="match_parent"
    6. android:layout_height="match_parent"
    7. android:orientation="vertical"
    8. tools:context=".LoginActivity">
    9. <LinearLayout
    10. android:layout_width="match_parent"
    11. android:layout_height="wrap_content"
    12. android:gravity="center"
    13. android:layout_marginTop="50dp">
    14. <ImageView
    15. android:layout_width="64dp"
    16. android:layout_height="64dp"
    17. android:background="@mipmap/box"
    18. >ImageView>
    19. <TextView
    20. android:layout_width="wrap_content"
    21. android:layout_height="wrap_content"
    22. android:text="智能药箱"
    23. android:textColor="@color/colorBlack"
    24. android:textSize="30dp"
    25. >TextView>
    26. LinearLayout>
    27. <LinearLayout
    28. android:layout_width="match_parent"
    29. android:layout_height="wrap_content"
    30. android:layout_marginTop="20dp"
    31. android:layout_marginLeft="30dp"
    32. android:layout_marginRight="30dp"
    33. android:background="@drawable/user_background">
    34. <TextView
    35. android:layout_width="wrap_content"
    36. android:layout_height="wrap_content"
    37. android:layout_gravity="center"
    38. android:text="用户名:"
    39. android:layout_marginLeft="10dp"
    40. android:textSize="25sp"
    41. >TextView>
    42. <EditText
    43. android:id="@+id/user_name"
    44. android:layout_width="match_parent"
    45. android:layout_height="60dp"
    46. android:layout_marginRight="30dp"
    47. android:textColor="@color/colorBlack"
    48. android:maxLines="1"
    49. >EditText>
    50. LinearLayout>
    51. <LinearLayout
    52. android:layout_width="match_parent"
    53. android:layout_height="wrap_content"
    54. android:layout_marginTop="20dp"
    55. android:layout_marginLeft="30dp"
    56. android:layout_marginRight="30dp"
    57. android:background="@drawable/user_background">
    58. <TextView
    59. android:layout_width="wrap_content"
    60. android:layout_height="wrap_content"
    61. android:layout_gravity="center"
    62. android:text="密 码:"
    63. android:layout_marginLeft="10dp"
    64. android:textSize="25sp"
    65. >TextView>
    66. <EditText
    67. android:id="@+id/user_password"
    68. android:layout_width="match_parent"
    69. android:layout_height="60dp"
    70. android:layout_marginRight="30dp"
    71. android:textColor="@color/colorBlack"
    72. android:maxLines="1"
    73. android:inputType="textPassword"
    74. >EditText>
    75. LinearLayout>
    76. <LinearLayout
    77. android:layout_width="match_parent"
    78. android:layout_height="wrap_content"
    79. android:layout_marginTop="40dp"
    80. android:orientation="horizontal">
    81. <Button
    82. android:id="@+id/denglu"
    83. android:layout_width="0dp"
    84. android:layout_weight="1"
    85. android:layout_marginLeft="25dp"
    86. android:layout_height="wrap_content"
    87. android:text="登录"
    88. android:textSize="25sp"
    89. android:layout_gravity="center"
    90. >Button>
    91. <Button
    92. android:id="@+id/zhuce"
    93. android:layout_width="0dp"
    94. android:layout_weight="1"
    95. android:layout_marginRight="25dp"
    96. android:layout_marginLeft="50dp"
    97. android:layout_height="wrap_content"
    98. android:text="注册"
    99. android:textSize="25dp"
    100. >Button>
    101. LinearLayout>
    102. LinearLayout>

    第三步、最终效果

     5、编写注册界面和登录界面的代码

    第一步、在com.example.myapplication文件夹下,新建一个Java Class文件

     命名为Mysql

     将内部的代码全部替换为以下代码

    1. package com.example.myapplication;
    2. import android.content.Context;
    3. import android.database.sqlite.SQLiteDatabase;
    4. import android.database.sqlite.SQLiteOpenHelper;
    5. import androidx.annotation.Nullable;
    6. //数据库
    7. public class Mysql extends SQLiteOpenHelper {
    8. public Mysql(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
    9. super(context, name, factory, version);
    10. }
    11. @Override
    12. public void onCreate(SQLiteDatabase db) {
    13. String sql = "create table logins(id integer primary key autoincrement,usname text,uspwd text)";
    14. db.execSQL(sql);
    15. }
    16. @Override
    17. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    18. }
    19. }

    替换后的结果

    第二步、编辑RegisterActivity.java文件内部的代码,替换为以下代码

    1. package com.example.myapplication;
    2. import androidx.appcompat.app.AppCompatActivity;
    3. import android.content.ContentValues;
    4. import android.content.Intent;
    5. import android.content.SharedPreferences;
    6. import android.database.sqlite.SQLiteDatabase;
    7. import android.os.Bundle;
    8. import android.view.View;
    9. import android.widget.Button;
    10. import android.widget.EditText;
    11. import android.widget.Toast;
    12. public class RegisterActivity extends AppCompatActivity {
    13. //注册界面的控件
    14. Button zhuce_success;//注册界面的按键
    15. EditText zhuce_user_name;//注册界面的用户名
    16. EditText zhuce_user_password; //注册界面的密码
    17. EditText zhuce_user_password_again; //注册界面的密
    18. Mysql mysql;
    19. SQLiteDatabase db;
    20. @Override
    21. protected void onCreate(Bundle savedInstanceState) {
    22. super.onCreate(savedInstanceState);
    23. setContentView(R.layout.activity_register);
    24. //寻找控件ID
    25. zhuce_success = this.findViewById(R.id.zhuce_success);
    26. zhuce_user_name = this.findViewById(R.id.zhuce_user_name);
    27. zhuce_user_password = this.findViewById(R.id.zhuce_user_password);
    28. zhuce_user_password_again= this.findViewById(R.id.zhuce_user_password_again);
    29. mysql = new Mysql(this,"Userinfo",null,1); //建数据库
    30. db = mysql.getReadableDatabase();
    31. zhuce_success.setOnClickListener(new View.OnClickListener() {
    32. @Override
    33. public void onClick(View view) {
    34. //保存注册数据的字符串
    35. String name = zhuce_user_name.getText().toString(); //用户名
    36. String pwd01 = zhuce_user_password.getText().toString(); //密码
    37. String pwd02 = zhuce_user_password_again.getText().toString(); //二次输入的密码
    38. //判断注册内容
    39. if(name.equals("")||pwd01 .equals("")||pwd02.equals("")){
    40. //显示弹窗
    41. Toast.makeText(getApplicationContext(),"用户名或密码不能为空!!",Toast.LENGTH_SHORT).show();
    42. }
    43. else {
    44. //如果注册时第一次输入的密码和第二次输入的密码一致
    45. if(pwd01.equals(pwd02)){
    46. //ContentValues是一种基本的存储类型
    47. ContentValues cv = new ContentValues();
    48. //放入数据
    49. cv.put("usname",name);
    50. cv.put("uspwd",pwd01);
    51. db.insert("logins",null,cv);
    52. //从当前界面跳转到登录页面
    53. Intent intent = new Intent();
    54. intent.setClass(RegisterActivity.this,LoginActivity.class);
    55. startActivity(intent);
    56. //弹窗
    57. Toast.makeText(getApplicationContext(),"账号注册成功!!",Toast.LENGTH_SHORT).show();
    58. }
    59. else {
    60. Toast.makeText(getApplicationContext(),"两次输入的密码不一致!!",Toast.LENGTH_SHORT).show();
    61. }
    62. }
    63. }
    64. });
    65. }
    66. }

    RegisterActivity.java文件替换后的界面

     第三步、编辑LoginActivity.java文件内部的代码,替换为以下代码

    1. package com.example.myapplication;
    2. import androidx.appcompat.app.AppCompatActivity;
    3. import android.content.Intent;
    4. import android.database.Cursor;
    5. import android.database.sqlite.SQLiteDatabase;
    6. import android.os.Bundle;
    7. import android.view.View;
    8. import android.widget.Button;
    9. import android.widget.EditText;
    10. import android.widget.Toast;
    11. public class LoginActivity extends AppCompatActivity {
    12. //声明控件
    13. //登陆界面的控件
    14. EditText user_name;
    15. EditText user_password;
    16. Button denglu;
    17. Button zhuce;
    18. //声明数据库
    19. Mysql mysql;
    20. SQLiteDatabase db;
    21. @Override
    22. protected void onCreate(Bundle savedInstanceState) {
    23. super.onCreate(savedInstanceState);
    24. setContentView(R.layout.activity_login);
    25. //找到当且xml文件内的控件ID
    26. //数据编辑框的ID
    27. user_name = this.findViewById(R.id.user_name);
    28. user_password = this.findViewById(R.id.user_password);
    29. //按键属性的ID
    30. denglu = this.findViewById(R.id.denglu);
    31. zhuce = this.findViewById(R.id.zhuce);
    32. //取出数据库内的数据
    33. mysql = new Mysql(this,"Userinfo",null,1);
    34. db = mysql.getReadableDatabase();
    35. //登录按键按下之后处理的事情
    36. denglu.setOnClickListener(new View.OnClickListener() {
    37. @Override
    38. public void onClick(View view) {
    39. //需要获取的输入的用户名和密码
    40. String storage_username = user_name.getText().toString();//用户控件.得到数据.转换为字符串;
    41. String storage_userpassword = user_password.getText().toString();//用户控件.得到数据.转换为字符串;
    42. //查询用户名和密码相同的数据
    43. Cursor cursor = db.query("logins",new String[]{"usname","uspwd"}," usname=? and uspwd=?",
    44. new String[]{storage_username,storage_userpassword},null,null,null);
    45. int flag = cursor.getCount(); //查询出来的记录项的条数,若没有该用户则为0条
    46. //登录成功后响应的数据
    47. if (flag!=0){
    48. Toast.makeText(getApplicationContext(), "登录成功!", Toast.LENGTH_SHORT).show();//显示登录成功的弹窗,简单写法
    49. Intent intent = null; //这个变量初始申明为空
    50. intent = new Intent(LoginActivity.this, MainActivity.class);
    51. startActivity(intent);
    52. }
    53. else {
    54. //假设正确的账号和密码分别是(VIP账号,没有写入数据库,无需注册账号)
    55. if (storage_username.equals("DPT") && storage_userpassword.equals("123456")) {
    56. //如果正确
    57. Toast.makeText(getApplicationContext(), "超级VIP登录成功!", Toast.LENGTH_SHORT).show();//显示登录成功的弹窗,简单写法
    58. Intent intent = null; //这个变量初始申明为空
    59. intent = new Intent(LoginActivity.this, MainActivity.class);
    60. startActivity(intent);
    61. }
    62. else{
    63. Toast.makeText(getApplicationContext(), "用户名输入错误或密码不正确,请重新登录!", Toast.LENGTH_SHORT).show();//获取显示的内容
    64. }
    65. }
    66. }
    67. });
    68. //注册按键按下之后,响应的事件
    69. zhuce.setOnClickListener(new View.OnClickListener() {
    70. @Override
    71. public void onClick(View view) {
    72. //实现界面跳转,从登录界面跳转到注册界面
    73. Intent intent = null; //这个变量初始申明为空
    74. intent = new Intent(LoginActivity.this, RegisterActivity.class);//跳转界面
    75. startActivity(intent);
    76. }
    77. });
    78. }
    79. }

    LoginActivity.java文件替换后的界面

    6、设置APP初始界面

    第一步、更改manifests文件夹下AndroidManifest.xml,如下

    第二步、设置APP图标和名称

     第三步、AndroidManifest.xml代码

    1. "1.0" encoding="utf-8"?>
    2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    3. xmlns:tools="http://schemas.android.com/tools"
    4. package="com.example.myapplication">
    5. <application
    6. android:allowBackup="true"
    7. android:icon="@mipmap/box"
    8. android:label="智能药箱"
    9. android:roundIcon="@mipmap/ic_launcher_round"
    10. android:supportsRtl="true"
    11. android:theme="@style/AppTheme"
    12. tools:ignore="GoogleAppIndexingWarning">
    13. <activity android:name=".RegisterActivity"/>
    14. <activity android:name=".LoginActivity" >
    15. <intent-filter>
    16. <action android:name="android.intent.action.MAIN" />
    17. <category android:name="android.intent.category.LAUNCHER" />
    18. intent-filter>
    19. activity>
    20. <activity android:name=".MainActivity">
    21. activity>
    22. application>
    23. manifest>

    7、连接手机,编译工程

    第一步、小米手机打开开发者模式,允许USB调试,允许USB安装。

    第二步、编译工程

    第三部分、总结

    1、参考资料

            我在实现这个功能的过程中也参考了很多篇博客,最推荐大家看的就是这一篇:

    (9条消息) Android studio 编写一个登录页面,并且具有注册功能_东尃的博客-CSDN博客_android studio登录注册界面实现

            然后关于寻找手机APP图标的网址:iconfont-阿里巴巴矢量图标库

            关于Android Studio开发过程中配色:网页设计常用色彩搭配表 | 网页配色表

    2、完整工程和代码

            这是博主的完整的代码,可以直接下载,但是你是不是要关注点赞收藏,然后再下载?👍👍👍我的APP工程

            有问题的小伙伴欢迎进Q群交流:1020775171

  • 相关阅读:
    别乱用了!文件写入的6种方法
    【Unity ShaderGraph】| 快速制作一个 钛金化不锈钢效果
    「Spring」Boot Docker 认证指南(上)
    链上物理资产「规模化」或将推动产业协作互联网迎来爆发
    B - Dungeon Master
    Spring Data JPA 中的分页和排序
    离子液体修饰的纳米金光学探针,荧光量子点
    Mysql多表练习题30道
    Delphi中关于PChar、Char数组、string[](ShortString)及结构体长度及占用空间的一些特性说明和测试
    常用 Git 命令
  • 原文地址:https://blog.csdn.net/Learning1232/article/details/127634795