Android studio登录注册页面和启动页面的实现

不多说了,直接放代码:

布局文件:
这里我把启动页放在了activity_main里:

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:background="@drawable/start"
    tools:context=".MainActivity"/>

activity_login:

<?xml version="1.0" encoding="utf-8"?>
<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=".loginActivity">


    <LinearLayout
        android:layout_width="409dp"
        android:layout_height="729dp"
        android:orientation="vertical"
        tools:layout_editor_absoluteX="1dp"
        tools:layout_editor_absoluteY="1dp">

        <EditText
            android:id="@+id/user"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:maxLines="2"
            android:hint="账号:"
            />

        <EditText
            android:id="@+id/password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            android:maxLines="2"
            android:hint="密码:"
            />

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

            <Button
                android:id="@+id/b1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Login"
                android:textAllCaps="false"/>

            <Button
                android:id="@+id/b2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Register"
                android:textAllCaps="false"/>
        </LinearLayout>
    </LinearLayout>


</androidx.constraintlayout.widget.ConstraintLayout>

activity_register:

<?xml version="1.0" encoding="utf-8"?>
<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=".registerActivity">

    <LinearLayout
        android:layout_width="409dp"
        android:layout_height="729dp"
        android:orientation="vertical"
        tools:layout_editor_absoluteX="1dp"
        tools:layout_editor_absoluteY="1dp">

        <EditText
            android:id="@+id/user1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="用户名:" />

        <EditText
            android:id="@+id/password1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="密码:" />
        <EditText
            android:id="@+id/password2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="重复密码:" />

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <Button
                android:id="@+id/b3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="注册"
                tools:layout_editor_absoluteX="150dp"
                tools:layout_editor_absoluteY="4dp" />
        </androidx.constraintlayout.widget.ConstraintLayout>
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity:

package com.example.mylogin;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.WindowManager;


import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);//隐藏状态栏

        setContentView(R.layout.activity_main );

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null)
        {
            actionBar.hide();
        }

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {

                Intent mainIntent = new Intent(MainActivity.this, loginActivity.class);//从启动界面跳转到登录界面
                startActivity(mainIntent);
                finish();
            }
        }, 3000);//系统休眠3秒
    }

}


loginActivity:

package com.example.mylogin;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class loginActivity extends AppCompatActivity {
    private String userName,psw,spPsw;
    private EditText user,password;//引入EditText控件


    @Override
    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        user=findViewById(R.id.user);
        password=findViewById(R.id.password);
        Button b1 = (Button) findViewById(R.id.b1);
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getText();
                spPsw=readPsw(userName);
                if(TextUtils.isEmpty(userName)){
                    Toast.makeText(loginActivity.this, "请输入用户名", Toast.LENGTH_SHORT).show();

                    return;
                }else if(TextUtils.isEmpty(psw)){
                    Toast.makeText(loginActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();

                    return;
                }else if(psw.equals(spPsw)){
                    Toast.makeText(loginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();

                    return;
                }else if((spPsw!=null&&!TextUtils.isEmpty(spPsw)&&!psw.equals(spPsw))){
                    Toast.makeText(loginActivity.this, "输入的用户名和密码不一致", Toast.LENGTH_SHORT).show();

                    return;
                }else{
                    Toast.makeText(loginActivity.this, "此用户名不存在", Toast.LENGTH_SHORT).show();
                }
            }
        });

        Button b2 = (Button) findViewById(R.id.b2);
        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(loginActivity.this,registerActivity.class);//从登录页面跳转到注册页面
                startActivity(intent);
            }
        });
    }

    private void getText(){
        userName=user.getText().toString().trim();
        psw=password.getText().toString().trim();
    }


    private String readPsw(String userName){

        SharedPreferences sp=getSharedPreferences("loginInfo", MODE_PRIVATE);
        return sp.getString(userName , "");
    }


   protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);
        if(data!=null){
            //是获取注册界面回传过来的用户名

            String userName=data.getStringExtra("userName");
            if(!TextUtils.isEmpty(userName)){

            }
        }
    }

}


registerActivity:

package com.example.mylogin;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class registerActivity extends AppCompatActivity {

    private EditText user1,password1,password2;
    private String userName,passWord1,passWord2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        user1=findViewById(R.id.user1);
        password1=findViewById(R.id.password1);
        password2=findViewById(R.id.password2);
        Button b3 = (Button)findViewById(R.id.b3);
        b3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getEditText();//获取EditText控件上的文本

                if(TextUtils.isEmpty(userName)){
                    Toast.makeText(registerActivity.this, "请输入用户名", Toast.LENGTH_SHORT).show();
                    return;
                }else if(TextUtils.isEmpty(passWord1)){
                    Toast.makeText(registerActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }else if(TextUtils.isEmpty(passWord2)){
                    Toast.makeText(registerActivity.this, "请再次输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }else if(!passWord1.equals(passWord2)) {
                    Toast.makeText(registerActivity.this, "输入两次的密码不一样", Toast.LENGTH_SHORT).show();
                    return;
                }else if (isExitUserName(userName)){
                    Toast.makeText(registerActivity.this,"此账户已经存在",Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(registerActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                    //把账号、密码和账号标识保存到sp里面
                    /**
                     * 保存账号和密码到SharedPreferences中
                     */
                    saveRegisterInfo(userName, passWord1);
                    Intent data = new Intent();
                    data.putExtra("userName", userName);
                    setResult(RESULT_OK, data);

                    registerActivity.this.finish();
                }

            }
        });
    }

    //读取EditText中的文本内容
    private void getEditText(){
        userName=user1.getText().toString().trim();
        passWord1=password1.getText().toString().trim();
        passWord2=password2.getText().toString().trim();
    }

    //将账号密码保存到SharePreferences
    private void saveRegisterInfo(String userName,String psw){
        SharedPreferences sp=getSharedPreferences("loginInfo", MODE_PRIVATE);
        SharedPreferences.Editor editor=sp.edit();
        editor.putString(userName, psw);
        editor.commit();
    }

    //从SharePreferen中读取用户名,判断用户名是否已经被注册
    private boolean isExitUserName(String userName){
        boolean exit_userName=false;
        SharedPreferences sp = getSharedPreferences("loginInfo",MODE_PRIVATE);
        String psw=sp.getString(userName,"");

        if(!TextUtils.isEmpty(psw)){
            exit_userName=true;
        }
        return exit_userName;
    }


}






你可能感兴趣的:(java,android,apk,android,android,studio,java)