首页 > 分享 > 用户注册与登录

用户注册与登录

设计用户登录界面、登录成功界面、用户注册界面,用户注册时,将其用户名、密码保存到SharedPreference中,登录时输入用户名、密码,读取SharedPreference,读取不到该用户名提示用户不存在,用户名读取验证通过后,读取用户密码,验证密码是否正确,密码不正确,提示密码不正确,密码验证通过,切换到登录成功界面,显示欢迎提示。

登陆界面(主界面) activity_main.xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="用户名:"
            android:textColor="#FF0000"
            android:gravity="left">
        </TextView>
        <EditText
            android:id="@+id/edit_username"
            android:layout_width="350dp"
            android:layout_height="wrap_content"
            android:hint="请输入用户名..."
            android:gravity="center">
        </EditText>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密     码:"
            android:textColor="#FF0000"
            android:gravity="left">
        </TextView>
        <EditText
            android:id="@+id/edit_password"
            android:layout_width="350dp"
            android:layout_height="wrap_content"
            android:hint="请输入密码..."
            android:inputType="textPassword"
            android:gravity="center">
        </EditText>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <Button
            android:id="@+id/btn_login"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="登  录"
            android:textSize="30sp"
            android:textColor="#FFFFFF"
            android:background="#134E7C"/>
        <Button
            android:id="@+id/btn_register"
            android:layout_marginTop="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="注册"
            android:layout_gravity="center"
            android:textSize="20sp"
            android:textColor="#000"
            android:background="#FFFFFF"/>
    </LinearLayout>
</LinearLayout>

注册界面 act_register.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="用户名:"
            android:textColor="#FF0000"
            android:gravity="left"/>
        <EditText
            android:id="@+id/Redit_username"
            android:layout_width="350dp"
            android:layout_height="wrap_content"
            android:hint="请输入用户名..."
            android:gravity="center">
        </EditText>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密     码:"
            android:textColor="#FF0000"
            android:gravity="left">
        </TextView>
        <EditText
            android:id="@+id/Redit_password"
            android:layout_width="350dp"
            android:layout_height="wrap_content"
            android:hint="请输入密码..."
            android:gravity="center">
        </EditText>
    </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <Button
                android:id="@+id/btn_rt"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="注  册"
                android:textSize="30sp"
                android:textColor="#FFFFFF"
                android:background="#134E7C"/>
    </LinearLayout>
</LinearLayout>

登陆成功界面login_ok.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="登录成功,欢迎进来!"
        android:textColor="#FF0000"
        android:textSize="50dp"/>

</LinearLayout>

java代码:

登陆界面对应的MainActivity.java:

package com.example.test1;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.test1.R;

import java.security.AllPermission;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initWidget();
        Login();
    }
/*
注册用户
 */
    private void initWidget() {
        Button btnRegister = (Button) findViewById(R.id.btn_register);
        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,
                        Register.class);//定义Intent变量,设置将要跳转的Activity
                startActivity(intent);//调用startActivity启动另外一个Activity
            }
        });
    }

    private void Login() {
        SharedPreferences preferences = getSharedPreferences("shared", MODE_PRIVATE);
        final String data_username = preferences.getString("username", "");
        final String data_password = preferences.getString("password", "");
        Button button = (Button) findViewById(R.id.btn_login);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText username = (EditText) findViewById(R.id.edit_username);
                EditText password = (EditText) findViewById(R.id.edit_password);
                String user = username.getText().toString();
                String pass = password.getText().toString();
                if(user.equals(data_username)){
                    if(pass.equals(data_password)) {
                        Intent intent0 = new Intent(MainActivity.this, Okey.class);
                        startActivity(intent0);
                    }else{
                        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                        builder.setMessage("密码错误,请重新输入!");
                        builder.setPositiveButton("OK", null);
                        builder.create().show();
                    }
                }else{
                    AlertDialog.Builder builder =new AlertDialog.Builder(MainActivity.this);
                    builder.setMessage("用户不存在,请注册!");
                    builder.setPositiveButton("OK",null);
                    builder.create().show();
                }
            }
        });
    }
}

注册界面对应的Register.java:

package com.example.test1;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Register extends Activity {
    private SharedPreferences sharedPreferences;
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_register);
        init();
    }

    private void init() {
        Button btn = (Button)findViewById(R.id.btn_rt);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sharedPreferences = getSharedPreferences("shared",MODE_PRIVATE);
                EditText name = (EditText)findViewById(R.id.Redit_username);
                EditText word = (EditText)findViewById(R.id.Redit_password);
                String username = name.getText().toString();
                String password = word.getText().toString();
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putString("username",username);//将username写入SharedPreferences中
                editor.putString("password",password);//将password写入SharedPreferences中
                editor.commit();//提交
                Toast.makeText(Register.this,"注册成功,请登录!",Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(Register.this,MainActivity.class);
                startActivity(intent);
                Register.this.finish();
            }
        });
    }
}

登陆成功界面对应的Okey.java:

package com.example.test1;

import android.app.Activity;
import android.os.Bundle;

public class Okey extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_ok);
    }

相关知识

用户登录
登录/注册
用户登录界面设计代码html
会员登录
登录界面UI设计指南,跟着大厂学设计!
花店登录页面设计
网上花店系统需求规格:注册、查询、订购与管理
移动应用程序设计基础——用户登录实验
品种登录、品种审定与新品种保护(一):品种登录
AI 绘画新秀 Leonardo 保姆级注册、使用教程

网址: 用户注册与登录 https://m.huajiangbk.com/newsview948771.html

所属分类:花卉
上一篇: JavaWeb实现简单的用户注册
下一篇: 登录注册的业务逻辑流程(详细梳理