Android studio 编写一个登录页面,并且具有注册功能

登录与注册

功能要求

1、创建登录界面,点击注册按钮,弹出注册窗口。
2、创建注册窗口,输入用户名和密码,在SQLite中存储用户名和密码。
3、注册成功,跳转到登录界面,进行登录。
4、注册成功,把用户名和密码保存到SharedPreferences中,登录时自动填充用户名和密码。

具体实现

初步思路

登录页面具有两个输入框和两个按键,输入框分别用于获取用户名和密码,按键分为登录按键和注册按键,分别跳往不同的页面。登陆成功后,进入到欢迎界面。注册跳转到注册页面,注册成功后,将数据存储到SharedPreferences和数据中,返回登录页面将SharedPreferences中的数据填充到输入框中。

涉及的代码文件

  1. MainActivity.java :主界面
  2. Mysql.java:利用sqlite的SQLiteOpenHelper类创建数据库
  3. Register.java:注册页面
  4. Welcome.java:登陆成功后的欢迎界面:
  5. activity_main.xml:登录页面的布局文件
  6. activity_register.xml:注册页面的布局文件
  7. activity_welcome.xml:欢迎页面的布局文件
    一般在Android studio 中创建Activity都会自动在AndroidManifest.xml中好,本次代码中也不需要对AndroidManifest.xml做任何更改。

实现效果

1登录页面:
这是我之前注册过后的账户,所以点击进去就自动填充好了信息。

Android studio 编写一个登录页面,并且具有注册功能
2.注册页面:
注册页面写的非常简略,如果想实现更多的效果可以网上查查,也可以参照下我上一篇文章中的注册页面。
编写一个简单的andriod注册页面,并跳转后显示注册信息
Android studio 编写一个登录页面,并且具有注册功能
3.注册成功后:
注册成功后会自动跳转到登录页面,并且填充信息。
Android studio 编写一个登录页面,并且具有注册功能
4.登录成功后:
登陆成功后会将用户名取出来,并显示欢迎。
Android studio 编写一个登录页面,并且具有注册功能

; 代码文件

1.登录界面——MainActivity.java :

这里登录比对数据的时候,我采用的方法是从数据库中查询与输入的用户名、密码相同的记录,若记录存在则登陆成功。不存在则提示用户用户名或密码输入错误。

package com.example.test06;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    EditText name,pwd;
    Button btnlogin,btnreg;
    Mysql mysql;
    SQLiteDatabase db;
    SharedPreferences sp1,sp2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        name = this.findViewById(R.id.name);
        pwd = this.findViewById(R.id.pwd);
        btnlogin = this.findViewById(R.id.login);
        btnreg = this.findViewById(R.id.reg);
        sp1 =  this.getSharedPreferences("useinfo",this.MODE_PRIVATE);
        sp2 = this.getSharedPreferences("username",this.MODE_PRIVATE);

        name.setText(sp1.getString("usname",null));
        pwd.setText(sp1.getString("uspwd",null));
        mysql = new Mysql(this,"Userinfo",null,1);
        db = mysql.getReadableDatabase();
        btnlogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = name.getText().toString();
                String password = pwd.getText().toString();

                Cursor cursor = db.query("logins",new String[]{"usname","uspwd"}," usname=? and uspwd=?",new String[]{username,password},null,null,null);

                int flag = cursor.getCount();
                if(flag!=0){
                    Intent intent = new Intent();
                    intent.setClass(MainActivity.this,Welcome.class);
                    SharedPreferences.Editor editor = sp2.edit();
                    cursor.moveToFirst();
                    String loginname = cursor.getString(0);
                    editor.putString("Loginname",loginname);
                    editor.commit();
                    startActivity(intent);
                }
                else{
                    Toast.makeText(MainActivity.this,"用户名或密码错误!",Toast.LENGTH_LONG).show();
                }

            }
        });

        btnreg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent();
                intent.setClass(MainActivity.this,Register.class);
                startActivity(intent);
                Toast.makeText(MainActivity.this,"前往注册!",Toast.LENGTH_SHORT).show();
            }
        });
    }
}

这是对应的布局文件activity_main.xml


<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="horizontal">
            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.3"
                android:gravity="center"
                android:textSize="18dp"
                android:text="用户名:"/>
            <EditText
                android:id="@+id/name"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_marginRight="20dp"/>
        LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="horizontal">
            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:textSize="18dp"
                android:layout_weight="0.3"
                android:text="密    码:"/>
            <EditText
                android:id="@+id/pwd"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:inputType="textPassword"
                android:layout_weight="1"
                android:layout_marginRight="20dp"
                />
        LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <Button
                android:id="@+id/login"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_margin="5dp"
                android:text="登录"
                />
            <Button
                android:id="@+id/reg"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_margin="5dp"
                android:text="注册"
                />
        LinearLayout>
    LinearLayout>

androidx.constraintlayout.widget.ConstraintLayout>

2.注册界面——Register.java

注册时,会先对用户名进行比对,若用户名存在则提醒用户名已存在。设置密码会比对两次输入的密码是否相同,不相同则发出提醒。

package com.example.test06;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Register extends AppCompatActivity {
    EditText usename,usepwd,usepwd2;
    Button submit;
    Mysql mysql;
    SQLiteDatabase db;
    SharedPreferences sp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        usename = this.findViewById(R.id.usename);
        usepwd =  this.findViewById(R.id.usepwd);
        usepwd2 = this.findViewById(R.id.usepwd2);
        submit =   this.findViewById(R.id.submit);
        mysql = new Mysql(this,"Userinfo",null,1);
        db = mysql.getReadableDatabase();
        sp = this.getSharedPreferences("useinfo",this.MODE_PRIVATE);

        submit.setOnClickListener(new View.OnClickListener() {
            boolean flag = true;
            @Override
            public void onClick(View v) {
                String name = usename.getText().toString();
                String pwd01 = usepwd.getText().toString();
                String pwd02 = usepwd2.getText().toString();
                String sex = "";
                if(name.equals("")||pwd01 .equals("")||pwd02.equals("")){
                    Toast.makeText(Register.this, "用户名或密码不能为空!!", Toast.LENGTH_LONG).show();
                }
                else{
                    Cursor cursor = db.query("logins",new String[]{"usname"},null,null,null,null,null);

                    while (cursor.moveToNext()){
                        if(cursor.getString(0).equals(name)){
                            flag = false;
                            break;
                        }
                    }
                    if(flag==true){
                        if (pwd01.equals(pwd02)) {
                            ContentValues cv = new ContentValues();
                            cv.put("usname",name);
                            cv.put("uspwd",pwd01);
                            db.insert("logins",null,cv);
                            SharedPreferences.Editor editor = sp.edit();
                            editor.putString("usname",name);
                            editor.putString("uspwd",pwd01);
                            editor.commit();
                            Intent intent = new Intent();
                            intent.setClass(Register.this,MainActivity.class);
                            startActivity(intent);
                            Toast.makeText(Register.this, "注册成功!", Toast.LENGTH_LONG).show();
                        }
                        else {
                            Toast.makeText(Register.this, "密码不一致!", Toast.LENGTH_LONG).show();
                        }
                    }
                    else{
                        Toast.makeText(Register.this, "用户已存在!", Toast.LENGTH_LONG).show();
                    }

                }
            }

        });
    }
}

这是对应的布局文件activity_register.xml


<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Register">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="用户名:" />

    <EditText
        android:id="@+id/usename"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text=""
        />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="密 码:"
        />

    <EditText
        android:id="@+id/usepwd"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"

        />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="确认密码:"
        />

    <EditText
        android:id="@+id/usepwd2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        />
        <Button
            android:id="@+id/submit"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="注册" />
    LinearLayout>
androidx.constraintlayout.widget.ConstraintLayout>

3.欢迎界面——Welcome.java

package com.example.test06;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.TextView;

public class Welcome extends AppCompatActivity {
    SharedPreferences sp;
    TextView showhello;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        sp = this.getSharedPreferences("username", this.MODE_PRIVATE);
        showhello = this.findViewById(R.id.mainword);

        showhello.setText("欢迎你!"+sp.getString("Loginname",""));
    }
}

这是对应的布局文件activity_welcome.xml


<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Welcome">
    <TextView
        android:id="@+id/mainword"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:textSize="22dp"
        tools:ignore="MissingConstraints" />
androidx.constraintlayout.widget.ConstraintLayout>

4.数据库——Mysql.java

package com.example.test06;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

public class Mysql extends SQLiteOpenHelper {
    public Mysql(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = "create table logins(id integer primary key autoincrement,usname text,uspwd text)";
        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

代码可能有些简陋,考虑也可能没那么全面,但基本的功能还是可以的。暂时没发现什么大的问题。

Original: https://blog.csdn.net/qq_45013261/article/details/117595619
Author: 东尃
Title: Android studio 编写一个登录页面,并且具有注册功能

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/816349/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球