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);
        }

    }
}

文末

大多数的开发者都有想要自我提升的意识,只是在学习过程中找不到合适的方法,缺少专业的指导,容易半途而废,最终收获甚少,甚至开始自我怀疑,很难在短时间内学有所成。那么,作为Android 开发者,该怎样规划自己的学习路线,向中高级进阶呢?

这里分享一份Android进阶学习资料需要的扫描下方二维码免费领取!!!

一、架构师筑基必备技能

1.深入理解Java泛型 2.注解深入浅出 3.并发编程 4.数据传输与序列化 5.Java虚拟机原理 6.高效IO ……img

二、Android百大框架源码解析

1.Retrofit 2.0源码解析 2.Okhttp3源码解析 3.ButterKnife源码解析 4.MPAndroidChart 源码解析 5.Glide源码解析 6.Leakcanary 源码解析 7.Universal-lmage-Loader源码解析 8.EventBus 3.0源码解析 9.zxing源码分析 10.Picasso源码解析 11.LottieAndroid使用详解及源码解析 12.Fresco 源码分析——图片加载流程

三、Android性能优化实战解析

1.腾讯Bugly:对字符串匹配算法的一点理解

2.爱奇艺:安卓APP崩溃捕获方案——xCrash

3.字节跳动:深入理解Gradle框架之一:Plugin, Extension, buildSrc

4.百度APP技术:Android H5首屏优化实践

5.支付宝客户端架构解析:Android 客户端启动速度优化之「垃圾回收」

6.携程:从智行 Android 项目看组件化架构实践

7.网易新闻构建优化:如何让你的构建速度“势如闪电”?

四、高级kotlin强化实战

1.Kotlin入门教程 2.Kotlin 实战避坑指南 3.项目实战《Kotlin Jetpack 实战》

​ ● 从一个膜拜大神的 Demo 开始

​ ● Kotlin 写 Gradle 脚本是一种什么体验?

​ ● Kotlin 编程的三重境界

​ ● Kotlin 高阶函数

​ ● Kotlin 泛型

​ ● Kotlin 扩展

​ ● Kotlin 委托

​ ● 协程“不为人知”的调试技巧

​ ● 图解协程:suspendimg

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值