Android開發-SQLite註冊登錄模塊(登錄)

Android開發-SQLite註冊登錄模塊(登錄)

前言

上一篇博文講了一下如何將用戶信息添加進入SQLite數據庫,實現註冊功能,這一次就來講講如何驗證用戶信息是否一致,完成登錄功能。

繪製佈局

線性佈局:用戶名輸入框(EditText)+密碼輸入框(EditText)+登錄按鈕(ImageView)

<?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"
    android:background="@drawable/login_bg">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <ImageView
            android:id="@+id/back_bt"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="@drawable/back"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:textSize="20dp"
            android:textColor="#FF9D00"
            android:gravity="center"
            android:text="登錄"
            />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#FF9D00">
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="center_horizontal"
        android:layout_marginTop="100dp"

        >
        <EditText
            android:id="@+id/ed_us"
            android:layout_width="300dp"
            android:layout_height="50dp"
            android:hint="請輸入用戶名/手機號/郵箱"
            android:background="@drawable/login_ed"
            android:textCursorDrawable="@drawable/guangbiao"
            />
        <EditText
            android:id="@+id/ed_pa"
            android:layout_width="300dp"
            android:layout_height="50dp"
            android:hint="請輸入密碼"
            android:inputType="textPassword"
            android:background="@drawable/login_ed"
            android:textCursorDrawable="@drawable/guangbiao"
            android:layout_marginTop="20dp"/>
        <ImageView
            android:id="@+id/login_bt"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_marginTop="20dp"
            android:background="@drawable/log_bt"
            />
      <LinearLayout
            android:layout_width="300dp"
            android:layout_height="50dp"
            android:orientation="horizontal">
        <TextView
             android:id="@+id/res"
             android:layout_width="150dp"
             android:layout_height="wrap_content"
             android:textColor="#FF9D00"
             android:text="註冊賬號"
        />
          <TextView
              android:id="@+id/wj"
              android:gravity="right"
              android:textColor="#FF9D00"
              android:layout_width="150dp"
              android:layout_height="wrap_content"
              android:text="忘記密碼?"
              />

      </LinearLayout>
        </LinearLayout>

</LinearLayout>

實現功能

你還記得在上一遍我不是在 DBOpenHelper 中寫了 getAllData() 方法嗎,那我們可以定義一個數組以鍵值對形式接收其返回的數值,使用for循環進行匹配,返回matchtrue,還是false


import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    DBOpenHelper mDBOpenHelper;
    EditText userName,password;
    ImageView back, login;
    TextView wj,res;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        mDBOpenHelper = new DBOpenHelper(this);

        userName = findViewById(R.id.ed_us);
        password =findViewById(R.id.ed_pa);
        back =findViewById(R.id.back_bt);
        login = findViewById(R.id.login_bt);
        wj =findViewById(R.id.wj);
        res =findViewById(R.id.res);

        back.setOnClickListener(this);
        login.setOnClickListener(this);
        wj.setOnClickListener(this);
        res.setOnClickListener(this);



    }

    @Override
    public void onClick(View v) {
       switch (v.getId()){
        /**返回主頁按鈕 */
           case R.id.back_bt:{
               Intent main =new Intent(LoginActivity.this,MainActivity.class);
               startActivity(main);
               finish();

           }
           break;
            /**核心功能*/
           case R.id.login_bt:{
            /**獲取用戶和密碼框輸入的值 */
               String name = userName.getText().toString().trim();
               String passwored = password.getText().toString().trim();
                /**判斷是否爲空,你還可以在註冊頁面使用正則表達式寫更多的規則*/
               if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(passwored)) {
                /**數組接收值*/
                   ArrayList<User> data = mDBOpenHelper.getAllData();
                   boolean match = false;
                    /**for循環匹配,註冊也應該寫一個這樣的,判斷數據庫裏已經有了這樣一個用戶,不能使用相同的用戶名進行註冊 */
                   for(int i=0;i<data.size();i++) {
                       User user = data.get(i);
                       if (name.equals(user.getName()) && passwored.equals(user.getPassword())){
                           match = true;
                           break;
                       }else{
                           match = false;
                       }
                   }
                   if (match) {
                       Toast.makeText(this, "登錄成功", Toast.LENGTH_SHORT).show();
                       Intent intent = new Intent(this, MainActivity.class);
                       startActivity(intent);
                       finish();//銷燬此Activity
                   }else {
                       Toast.makeText(this, "用戶名或密碼不正確,請重新輸入"+passwored, Toast.LENGTH_SHORT).show();
                   }
               } else {
                   Toast.makeText(this, "請輸入你的用戶名或密碼", Toast.LENGTH_SHORT).show();
               }



           }
           break;
           case R.id.wj:{
               Toast.makeText(this,  "未開發", Toast.LENGTH_SHORT).show();

           }
           break;
           case R.id.res:{
               Intent res =new Intent(LoginActivity.this,UpActivity.class);
               startActivity(res);
               finish();

           }
           break;

       }



    }
}

效果圖

在這裏插入圖片描述
借鑑所得:…找不到網址了,勿怪。
源碼下載https://download.csdn.net/download/qq_41858698/12056886
上一篇註冊地址https://blog.csdn.net/qq_41858698/article/details/103753447
GitHub項目下載得等考試周結束了。
有了註冊登錄,應該還有找回密碼吧,但是得做個真的驗證碼驗證。

發佈了17 篇原創文章 · 獲贊 3 · 訪問量 1364
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章