关于安卓登录注册页面构建(包含用户名密码验证+记住密码+再按一次返回退出)

基本的登录注册页面

登录页面
  xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="5dp"
    android:paddingLeft="36dp"
    android:paddingRight="15dp"
    android:paddingTop="20dp"
    tools:context="com.sec.myapplication.LoginActivity">

    <ScrollView
        android:id="@+id/sv"
        android:layout_width="300dp"
        android:layout_height="500dp"
        tools:layout_editor_absoluteY="8dp"
        tools:layout_editor_absoluteX="8dp">
        <RelativeLayout
            android:id="@+id/login_view"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_width="match_parent">
            <Button
                android:layout_width="120dp"
                android:layout_height="wrap_content"
                android:text="登录"
                android:id="@+id/login_btn_login"
                android:onClick="finish_login"
                android:background="@drawable/selector_button"
                android:textSize="20dp"
                android:textColor="#ffffff"
                android:layout_below="@+id/login_edit_pwd"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_marginTop="52dp"
                android:layout_marginLeft="15dp"/>
            <Button
                android:layout_width="120dp"
                android:layout_height="wrap_content"
                android:text="注册"
                android:id="@+id/login_btn_register"
                android:onClick="finish_register"
                android:background="@drawable/selector_button"
                android:checked="true"
                android:textSize="20dp"
                android:textColor="#ffffff"
                android:layout_alignParentBottom="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"
                android:layout_marginRight="15dp"
                android:layout_marginEnd="15dp" />
            <EditText
                android:layout_width="400dp"
                android:layout_height="60dp"
                android:inputType="textPassword"
                android:ems="10"
                android:id="@+id/login_edit_pwd"
                android:drawableLeft="@android:drawable/ic_lock_idle_lock"
                android:hint="请输入您的密码"
                android:layout_below="@+id/login_edit_account"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true" />

            <CheckBox
                android:layout_width="100dp"
                android:layout_height="20dp"
                android:text="记住密码"
                android:id="@+id/Login_Remember"
                android:checked="false"
                android:textSize="15dp"
                android:layout_below="@+id/login_edit_pwd"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true" />

            <EditText
                android:id="@+id/login_edit_account"
                android:layout_width="400dp"
                android:layout_height="60dp"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/logo"
                android:layout_marginTop="25dp"
                android:drawableLeft="@android:drawable/ic_menu_myplaces"
                android:hint="请输入您的用户名"
                android:inputType="textPersonName"
                android:selectAllOnFocus="false"/>

            <ImageView
                android:id="@+id/logo"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_alignParentTop="true"
                android:layout_alignWithParentIfMissing="false"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="21dp"
                android:background="@drawable/icon_circle"
                android:src="@drawable/touxiang" />
        </RelativeLayout>
    </ScrollView>
</android.support.constraint.ConstraintLayout>

Java:

package com.sec.myapplication;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;


public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText userName;
    private EditText password;
    private Button loadButton;
    private Button registerButton;
    private myOpenHelper myOpenHelper;
    private SQLiteDatabase database;
    private CheckBox checkboxButton;
    private SharedPreferences sp = null;
    private SharedPreferences.Editor editor;
    private long exitTime = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_login);
        userName=(EditText)findViewById(R.id.login_edit_account);
        password=(EditText)findViewById(R.id.login_edit_pwd);
        loadButton = (Button) findViewById(R.id.login_btn_login);
        registerButton = (Button) findViewById(R.id.login_btn_register);
        checkboxButton = (CheckBox) findViewById(R.id.Login_Remember);
        sp = this.getSharedPreferences("userinfo", Context.MODE_PRIVATE);
        if (sp.getBoolean("checkboxBoolean", false))
        {
            userName.setText(sp.getString("userName", null));
            password.setText(sp.getString("password", null));
            checkboxButton.setChecked(true);
        }
        loadButton.setTag(1);
        loadButton.setOnClickListener(this);
        registerButton.setTag(2);
        registerButton.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        int tag = (int)v.getTag();
        switch(tag)
        {
            case 1:
                Intent intent1=new Intent(this,MainActivity.class);
                intent1.putExtra("userName",userName.getText().toString());
                intent1.putExtra("password",password.getText().toString());
                if (idCorrect()) {
                    intent1.putExtra("userName",userName.getText().toString());
                    intent1.putExtra("password",password.getText().toString());
                    startActivity(intent1);
                    finish();
                }
                break;
            case 2:
                Intent intent=new Intent(this,registerActivity.class);
                startActivity(intent);
                break;
        }
    }
    public boolean idCorrect(){
        String uname = userName.getText().toString();
        String upwd = password.getText().toString();
        if(uname.length() != 0 && upwd.length() != 0) {
            myOpenHelper = new myOpenHelper(this,"mydb.db",null,1);
            database = myOpenHelper.getWritableDatabase();
            String querySql = "select name,pwd from user";
            Cursor cursor = database.rawQuery(querySql,null);
            while (cursor.moveToNext()) {
                String userName1 = cursor.getString(cursor.getColumnIndex("name"));
                String password1 = cursor.getString(cursor.getColumnIndex("pwd"));
                if (userName1.equals(userName.getText().toString()) && password1.equals(password.getText().toString())) {
                    if (checkboxButton.isChecked()) {
                        editor = sp.edit();
                        editor.putString("userName", userName.getText().toString());
                        editor.putString("password", password.getText().toString());
                        editor.putBoolean("checkboxBoolean", true);
                        editor.commit();
                    }
                    else {
                        editor = sp.edit();
                        editor.putString("userName", null);
                        editor.putString("password", null);
                        editor.putBoolean("checkboxBoolean", false);
                        editor.commit();
                    }
                    cursor.close();
                    return true;
                }
            }
            Toast.makeText(this,"账号或密码错误,请重新输入",Toast.LENGTH_LONG).show();
            return false;
        }
        Toast.makeText(this,"账号或密码不能为空",Toast.LENGTH_LONG).show();
        return false;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
            if((System.currentTimeMillis()-exitTime) > 2000){
                Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}

注册页面:

xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="35dp"
    android:paddingTop="40dp"
    tools:context="com.sec.myapplication.registerActivity">
    <RelativeLayout
        android:layout_height="500dp"
        android:layout_width="305dp">
    <EditText
        android:drawableLeft="@android:drawable/ic_menu_myplaces"
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/resetpwd_edit_name"
        android:layout_alignParentTop="true"
        android:hint="请输入6~12位字母数字,字母开头"
        android:layout_alignLeft="@+id/resetpwd_edit_pwd_new"
        android:layout_alignStart="@+id/resetpwd_edit_pwd_new"
        android:layout_alignRight="@+id/resetpwd_edit_pwd_new"
        android:layout_alignEnd="@+id/resetpwd_edit_pwd_new" />

    <EditText
        android:drawableLeft="@android:drawable/ic_lock_idle_lock"
        android:layout_width="fill_parent"
        android:layout_height="60dp"
        android:inputType="textPassword"
        android:ems="10"
        android:id="@+id/resetpwd_edit_pwd_old"
        android:hint="请输入6~12位字母数字"
        android:layout_below="@+id/resetpwd_edit_name"
        android:layout_alignRight="@+id/resetpwd_edit_name"
        android:layout_alignEnd="@+id/resetpwd_edit_name"
        android:layout_alignLeft="@+id/resetpwd_edit_name"
        android:layout_alignStart="@+id/resetpwd_edit_name" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="取消"
        android:id="@+id/register_btn_cancel"
        android:textSize="20dp"
        android:textColor="#ffffff"
        android:background="@drawable/selector_button"
        android:layout_below="@+id/register_btn_sure"
        android:layout_alignLeft="@+id/register_btn_sure"
        android:layout_alignStart="@+id/register_btn_sure"
        android:layout_marginTop="10dp" />

    <EditText
        android:drawableLeft="@android:drawable/ic_lock_idle_lock"
        android:layout_width="fill_parent"
        android:layout_height="60dp"
        android:inputType="textPassword"
        android:ems="10"
        android:id="@+id/resetpwd_edit_pwd_new"
        android:layout_below="@+id/resetpwd_edit_pwd_old"
        android:layout_centerHorizontal="true"
        android:hint="请确认您的密码" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="注册"
        android:id="@+id/register_btn_sure"
        android:textSize="20dp"
        android:textColor="#ffffff"
        android:background="@drawable/selector_button"
        android:layout_below="@+id/resetpwd_edit_pwd_new"
        android:layout_alignLeft="@+id/resetpwd_edit_pwd_new"
        android:layout_alignStart="@+id/resetpwd_edit_pwd_new"
        android:layout_marginTop="20dp" />
    </RelativeLayout>
</android.support.constraint.ConstraintLayout>

Java:

package com.sec.myapplication;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class registerActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText editUser;
    private EditText pwd;
    private EditText pwdAgain;
    private Button register;
    private Button cancel;
    private myOpenHelper myOpenHelper;
    private SQLiteDatabase database;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        editUser=(EditText)findViewById(R.id.resetpwd_edit_name);
        pwd=(EditText)findViewById(R.id.resetpwd_edit_pwd_old);
        pwdAgain=(EditText)findViewById(R.id.resetpwd_edit_pwd_new);
        register= (Button) findViewById(R.id.register_btn_sure);
        cancel= (Button) findViewById(R.id.register_btn_cancel);
        register.setTag(1);
        register.setOnClickListener(this);
        cancel.setTag(2);
        cancel.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int Tag=(int)view.getTag();
        switch (Tag){
            case 1:
                createDatabase();
                insertData();
                break;
            case 2:
                Intent intent=new Intent(this,LoginActivity.class);
                startActivity(intent);
                break;
            default:
                Toast.makeText(registerActivity.this,"注册失败,请再试一次",Toast.LENGTH_LONG).show();
                break;
        }
    }

    public void insertData() {
        if(nameCorrect()){
            if(pwdCheck()){
                if(pwdCorrect()){
                    String insertSql = "insert into user (name,pwd) values ('" + editUser.getText().toString() + "','" + pwd.getText().toString() + "');";
                    database.execSQL(insertSql);
                    Toast.makeText(registerActivity.this,"注册成功",Toast.LENGTH_LONG).show();
                    Intent intent1=new Intent(this,LoginActivity.class);
                    startActivity(intent1);
                }
            }
        }
    }

    public void createDatabase(){
        myOpenHelper=new myOpenHelper(this,"mydb.db",null,1);
        database=myOpenHelper.getWritableDatabase();

    }
    public boolean nameCorrect(){
         String uname= editUser.getText().toString();
        if(uname.length() != 0){
            String reg = "^[a-zA-Z][a-zA-Z0-9_]{5,11}$";
            Pattern pattern = Pattern.compile(reg);
            Matcher matcher = pattern.matcher(uname);
            boolean b = matcher.matches();
            if(b){
                return true;
            }
             Toast.makeText(registerActivity.this,"用户名有误",Toast.LENGTH_LONG).show();
        }else{
            Toast.makeText(registerActivity.this,"用户名不能为空",Toast.LENGTH_LONG).show();
        }
        return false;
    }
    public boolean pwdCheck(){
        String upwd = pwd.getText().toString();
        if( upwd.length() != 0) {
            String reg = "^[a-zA-Z0-9]{6,12}$";
            Pattern pattern = Pattern.compile(reg);
            Matcher matcher = pattern.matcher(upwd);
            boolean b = matcher.matches();
            if(b){
                return true;
            }
            Toast.makeText(registerActivity.this,"密码有误",Toast.LENGTH_LONG).show();
            return false;
        }
        Toast.makeText(registerActivity.this,"密码不能为空",Toast.LENGTH_LONG).show();
        return false;
    }
    public boolean pwdCorrect(){
        if(pwd.getText().toString().equals(pwdAgain.getText().toString())){
            return true;
        }
        Toast.makeText(registerActivity.this,"两次密码输入不一致,请重新输入。",Toast.LENGTH_LONG).show();
        return false;
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wentianyunhe

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值