Loading

Android-每日打卡APP-实现登录功能

每日打卡APP新的进展-实现登录功能-昨天已经把注册功能实现了,今天也很快把登录功能做了出来,然后接着着手做其他功能,打卡功能写在下一篇博客

能够实现登录和注册,注册相关的代码我在之前的博客中写过了,这里主要是登录的部分!同时登录后自己的学号姓名信息会显示在自己的主页里!

我在CRUD.java中又添加了一个方法,用于查询注册后的学生的账户信息以便登录,这部分也是对应着SQLite中查询的部分

基本查询语句格式:SELECT * FROM table_name WHRER 条件表达式

先看效果,代码放后边

public  Student getStudentById(String id){

        SQLiteDatabase db = dbHelper.getReadableDatabase();
        String selectQuery = "SELECT "+
                Student.KEYWORD +","+
                Student.ID +","+
                Student.NAME +","+
                Student.GRADE + ","+
                Student.PHONE+
                " FROM " + Student.TABLE
                +" WHERE " +
                Student.ID + "=?";
        int count = 0;
        Student student = new Student();
        Cursor cursor = db.rawQuery(selectQuery,new String[]{String.valueOf(id)});
        if (cursor.moveToFirst()){
            do {
                student.keyword = cursor.getInt(cursor.getColumnIndex(Student.KEYWORD));
                student.id = cursor.getString(cursor.getColumnIndex(Student.ID));
                student.name = cursor.getString(cursor.getColumnIndex(Student.NAME));
                student.grade = cursor.getString(cursor.getColumnIndex(Student.GRADE));
                student.phone = cursor.getString(cursor.getColumnIndex(Student.PHONE));
            }while (cursor.moveToNext());
        }
        cursor.close();
        db.close();
        return student;
    }

然后是实现登录的代码

package com.example.clockappliction;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.clockappliction.DataBase.CRUD;
import com.example.clockappliction.Information.Student;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btn_login, btn_reg;

    private EditText et_log_id,et_log_name,et_log_grade,et_log_phone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn_login = (Button) findViewById(R.id.btn_login);
        btn_login.setOnClickListener(this);

        btn_reg = (Button) findViewById(R.id.btn_reg);
        btn_reg.setOnClickListener(this);

        et_log_id = (EditText) findViewById(R.id.et_log_id);
        et_log_name = (EditText) findViewById(R.id.et_log_name);
        et_log_grade = (EditText) findViewById(R.id.et_log_grade);
        et_log_phone = (EditText) findViewById(R.id.et_log_phone);

    }

    @Override
    public void onClick(View view) {

        if (view == findViewById(R.id.btn_login)){

            Student student = new Student();
            CRUD crud = new CRUD(this);
            //获取输入框的学生信息
            student.id = et_log_id.getText().toString();
            student.name = et_log_name.getText().toString();
            student.grade = et_log_grade.getText().toString();
            student.phone = et_log_phone.getText().toString();
            //通过id获取数据库学生信息
            Student studentData =crud.getStudentById(student.id);
            //登录
            if (student.id.equals(studentData.id)){
                if (student.name.equals(studentData.name)){
                    if (student.grade.equals(studentData.grade)){
                        if (student.phone.equals(studentData.phone)){
                            Intent intent =new Intent(this,MenuActivity.class);
                            intent.putExtra("st_id",student.id);
                            intent.putExtra("st_name",student.name);
                            startActivity(intent);
                        }else {
                            Toast.makeText(this, "电话号码不正确", Toast.LENGTH_SHORT).show();
                        }
                    }else {
                        Toast.makeText(this, "班级不正确", Toast.LENGTH_SHORT).show();
                    }
                }else {
                    Toast.makeText(this, "姓名不正确", Toast.LENGTH_SHORT).show();
                }
            }else {
                Toast.makeText(this, "学号不正确", Toast.LENGTH_SHORT).show();
            }

        } else if (view == findViewById(R.id.btn_reg)) {
            //跳转到登录页面
            Intent intent = new Intent(this,RegActivity.class);
            intent.putExtra("keys",0);
            startActivity(intent);
        }
    }

}

xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:orientation="vertical"
    android:background="#DDDDDD"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv_title"
        android:text="每日打卡APP"
        android:textSize="30sp"
        android:layout_margin="45dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentStart="true"
         />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:id="@+id/et_log_id"
        android:hint="学号"

        android:layout_below="@+id/tv_title"
        android:layout_marginTop="15dp"
        ></EditText>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:id="@+id/et_log_name"
        android:hint="姓名"

        android:layout_below="@+id/et_log_id"
        android:layout_marginTop="15dp"
        ></EditText>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:id="@+id/et_log_grade"
        android:hint="班级"

        android:layout_below="@+id/et_log_name"
        android:layout_marginTop="15dp"
        ></EditText>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:id="@+id/et_log_phone"
        android:hint="手机号码"

        android:layout_below="@+id/et_log_grade"
        android:layout_marginTop="15dp"
        ></EditText>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn_login"
        android:text="登录"
        android:textSize="30dp"
        android:layout_below="@+id/et_log_phone"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="20dp"
        ></Button>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn_reg"
        android:text="注册"
        android:textSize="30dp"
        android:layout_toRightOf="@+id/btn_login"
        android:layout_below="@+id/et_log_phone"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="20dp"
        ></Button>

</RelativeLayout>

MenuActivity.java

package com.example.clockappliction;

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

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class MenuActivity extends AppCompatActivity implements View.OnClickListener{

    private TextView tv_my_id,tv_my_name;
    private Button btn_mu_dk;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);
        btn_mu_dk = (Button) findViewById(R.id.btn_mu_dk);
        btn_mu_dk.setOnClickListener(this);

        tv_my_id = (TextView) findViewById(R.id.tv_my_id);
        tv_my_name = (TextView) findViewById(R.id.tv_my_name);
        Intent intent = getIntent();
        String st_id =intent.getStringExtra("st_id");
        String st_name =intent.getStringExtra("st_name");

        tv_my_id.setText("学号:"+ st_id);
        tv_my_name.setText("姓名:"+ st_name);


    }

    @Override
    public void onClick(View view) {


        if (view==findViewById(R.id.btn_mu_dk)){
            Intent intent = new Intent(this,ClockActivity.class);
            String cid = tv_my_id.getText().toString();
            intent.putExtra("cid",cid);
            startActivity(intent);
        }

    }
}

 

posted @ 2023-03-04 17:22  冰稀饭Aurora  阅读(49)  评论(0编辑  收藏  举报