首页 > 其他分享 >打卡App登录页面

打卡App登录页面

时间:2023-03-18 23:23:35浏览次数:35  
标签:name App private String import 打卡 password android 页面

package com.example.daka;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.service.autofill.UserData;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.daka.database.UserDBHelper;
import com.example.daka.enity.User;

import java.util.ArrayList;

public class LoginActivity extends AppCompatActivity implements View.OnFocusChangeListener {

    private EditText et_name;
    private EditText et_password;
    private Button btn_login;
    private Button btn_zuce;
    private UserDBHelper mHelper;
    private static final String DATABASE_NAME = "user.db";
    private static final int DATABASE_VERSION = 1;
    private static final String TABLE_NAME = "user_info";
    private SQLiteDatabase db;
    private Intent intent;

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

        UserDBHelper databaseHelper = UserDBHelper.getInstance(LoginActivity.this);
        db = databaseHelper.getWritableDatabase();

        mHelper = UserDBHelper.getInstance(this);
        db = databaseHelper.getWritableDatabase();

        btn_login = findViewById(R.id.btn_login);
        btn_zuce = findViewById(R.id.btn_zuce);

        et_name = findViewById(R.id.et_name);
        et_password = findViewById(R.id.et_password);

        btn_zuce.setOnClickListener(v -> {
            intent = new Intent(LoginActivity.this, ZuceActivity.class);
            startActivity(intent);
        });
        btn_login.setOnClickListener(new LoginListener());


    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            String name = et_name.getText().toString();
            if (TextUtils.isEmpty(name)) {
                et_name.requestFocus();
                Toast.makeText(this, "请输入用户名", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private class LoginListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            switch (v.getId()){

                case R.id.btn_login:
                    String name = et_name.getText().toString();
                    String password = et_password.getText().toString();
                    if ("".equals(name) || "".equals(password)) {
                        Toast.makeText(LoginActivity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
                    } else {
                        isUserinfo(name, password);
                    }
                    break;
                case R.id.btn_zuce:
                    Intent intent =new Intent();
                    intent.setClass(LoginActivity.this,ZuceActivity.class);
                    startActivity(intent);
            }
        }

    }

    private void isUserinfo(String name, String passwordString) {
        String passString = passwordString;
        try {
            @SuppressLint("Recycle") Cursor cursor = db.query(TABLE_NAME, new String[]{"name", "password"}, "name=?", new String[]{name}, null, null, "password");
            while (cursor.moveToNext()) {
                @SuppressLint("Range") String password = cursor.getString(cursor.getColumnIndex("password"));
                cursor.close();
                if (passString.equals(password)) {
                    new AlertDialog.Builder(LoginActivity.this).setTitle("正确")
                            .setMessage("成功登录").setPositiveButton("确定", (dialog, which) -> {
                                // TODO Auto-generated method stub
                                Intent a = new Intent(LoginActivity.this, AllActivity.class);
                                startActivity(a);
                            }).show();
                    break;
                } else {
                    Toast.makeText(this, "用户名密码不正确", Toast.LENGTH_LONG).show();
                    break;
                }
            }
        } catch (SQLiteException e) {
            CreatTable();
        }

    }

    private void CreatTable() {
        String sql= "CREATE TABLE IF NOT EXISTS " + TABLE_NAME
                + " (name varchar(30) primary key,password varchar(30));";
        try {
            db.execSQL(sql);
        } catch (SQLException ignored) {
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登录"
        android:gravity="center"
        android:textSize="25dp"
        android:textColor="#0567B5"
        android:background="#A6CDEC">
    </TextView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="55dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="用户名:"
            android:textSize="20dp"
            android:textColor="@color/black">
        </TextView>

        <EditText
            android:id="@+id/et_name"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:layout_marginTop="5dp"
            android:hint="请输入用户名"
            android:inputType="text"
            ></EditText>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="55dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="密    码:"
            android:textSize="20dp"
            android:textColor="@color/black">
        </TextView>

        <EditText
            android:id="@+id/et_password"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:layout_marginTop="5dp"
            android:hint="请输入密码"
            android:inputType="textPassword"
            ></EditText>
    </LinearLayout>

    <Button
        android:id="@+id/btn_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="25dp"
        android:text="登录"
        ></Button>
    <Button
        android:id="@+id/btn_zuce"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="25dp"
        android:text="注册"
        ></Button>
</LinearLayout>
package com.example.daka;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.service.autofill.UserData;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.daka.database.UserDBHelper;
import com.example.daka.enity.User;

import java.util.ArrayList;

public class LoginActivity extends AppCompatActivity implements View.OnFocusChangeListener {

    private EditText et_name;
    private EditText et_password;
    private Button btn_login;
    private Button btn_zuce;
    private UserDBHelper mHelper;
    private static final String DATABASE_NAME = "user.db";
    private static final int DATABASE_VERSION = 1;
    private static final String TABLE_NAME = "user_info";
    private SQLiteDatabase db;
    private Intent intent;

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

        UserDBHelper databaseHelper = UserDBHelper.getInstance(LoginActivity.this);
        db = databaseHelper.getWritableDatabase();

        mHelper = UserDBHelper.getInstance(this);
        db = databaseHelper.getWritableDatabase();

        btn_login = findViewById(R.id.btn_login);
        btn_zuce = findViewById(R.id.btn_zuce);

        et_name = findViewById(R.id.et_name);
        et_password = findViewById(R.id.et_password);

        btn_zuce.setOnClickListener(v -> {
            intent = new Intent(LoginActivity.this, ZuceActivity.class);
            startActivity(intent);
        });
        btn_login.setOnClickListener(new LoginListener());


    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            String name = et_name.getText().toString();
            if (TextUtils.isEmpty(name)) {
                et_name.requestFocus();
                Toast.makeText(this, "请输入用户名", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private class LoginListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            switch (v.getId()){

                case R.id.btn_login:
                    String name = et_name.getText().toString();
                    String password = et_password.getText().toString();
                    if ("".equals(name) || "".equals(password)) {
                        Toast.makeText(LoginActivity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
                    } else {
                        isUserinfo(name, password);
                    }
                    break;
                case R.id.btn_zuce:
                    Intent intent =new Intent();
                    intent.setClass(LoginActivity.this,ZuceActivity.class);
                    startActivity(intent);
            }
        }

    }

    private void isUserinfo(String name, String passwordString) {
        String passString = passwordString;
        try {
            @SuppressLint("Recycle") Cursor cursor = db.query(TABLE_NAME, new String[]{"name", "password"}, "name=?", new String[]{name}, null, null, "password");
            while (cursor.moveToNext()) {
                @SuppressLint("Range") String password = cursor.getString(cursor.getColumnIndex("password"));
                cursor.close();
                if (passString.equals(password)) {
                    new AlertDialog.Builder(LoginActivity.this).setTitle("正确")
                            .setMessage("成功登录").setPositiveButton("确定", (dialog, which) -> {
                                // TODO Auto-generated method stub
                                Intent a = new Intent(LoginActivity.this, AllActivity.class);
                                startActivity(a);
                            }).show();
                    break;
                } else {
                    Toast.makeText(this, "用户名密码不正确", Toast.LENGTH_LONG).show();
                    break;
                }
            }
        } catch (SQLiteException e) {
            CreatTable();
        }

    }

    private void CreatTable() {
        String sql= "CREATE TABLE IF NOT EXISTS " + TABLE_NAME
                + " (name varchar(30) primary key,password varchar(30));";
        try {
            db.execSQL(sql);
        } catch (SQLException ignored) {
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="注册"
        android:gravity="center"
        android:textSize="25dp"
        android:textColor="#0567B5"
        android:background="#A6CDEC">
    </TextView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="55dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="用  户  ID"
            android:textSize="20dp"
            android:textColor="@color/black">
        </TextView>

        <EditText
            android:id="@+id/et_ID"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:layout_marginTop="5dp"
            android:hint="请输入用户ID(学号)"
            android:inputType="text"
            ></EditText>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="55dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="密        码"
            android:textSize="20dp"
            android:textColor="@color/black">
        </TextView>

        <EditText
            android:id="@+id/et_password"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:layout_marginTop="5dp"
            android:hint="请设置密码"
            android:inputType="textPassword"
            ></EditText>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="55dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="用  户  名"
            android:textSize="20dp"
            android:textColor="@color/black">
        </TextView>

        <EditText
            android:id="@+id/et_name"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:layout_marginTop="5dp"
            android:hint="请输入用户名(姓名)"
            android:inputType="text"
            ></EditText>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="55dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="手机号码"
            android:textSize="20dp"
            android:textColor="@color/black">
        </TextView>

        <EditText
            android:id="@+id/et_phone"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:layout_marginTop="5dp"
            android:hint="请输入手机号码"
            android:inputType="text"
            ></EditText>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="55dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="用户单位"
            android:textSize="20dp"
            android:textColor="@color/black">
        </TextView>

        <EditText
            android:id="@+id/et_classroom"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:layout_marginTop="5dp"
            android:hint="请输入用户单位(班级)"
            android:inputType="text"
            ></EditText>
    </LinearLayout>

    <Button
        android:id="@+id/btn_zuce"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="25dp"
        android:text="注册"
        ></Button>

</LinearLayout>
package com.example.daka.database;


import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.example.daka.enity.Daka;
import com.example.daka.enity.User;

import java.util.ArrayList;

public class UserDBHelper extends SQLiteOpenHelper {

    //数据库名
    private static final String db_name="user.db";

    //表名
    private static final String table_name="user_info";


    //版本号
    private static final int db_version=1;

    //数据库帮助器的初定义
    private static UserDBHelper mHelper=null;

    //读写得初定义
    private SQLiteDatabase mRDB=null;
    private SQLiteDatabase mWDB=null;
    private SQLiteDatabase db;

    private UserDBHelper(Context context){
        super(context,db_name,null,db_version);

    }

    //利用单例模式获取数据库帮助器的唯一实例
    public static UserDBHelper getInstance(Context context){
        if(mHelper==null){
            mHelper=new UserDBHelper(context);
        }

        return mHelper;
    }

    //打开数据库得读操作
    public SQLiteDatabase openReadLink(){
        if(mRDB==null||!mRDB.isOpen()){
            mRDB=mHelper.getReadableDatabase();
        }

        return mRDB;
    }

    //打开数据库的写操作
    public SQLiteDatabase openWriteLink(){
        if(mWDB==null||!mWDB.isOpen()){
            mWDB=mHelper.getWritableDatabase();
        }

        return mWDB;
    }

    //关闭数据库连接
    public void closeLink(){
        if(mRDB!=null&&mRDB.isOpen()){
            mRDB.close();
            mRDB=null;
        }

        if(mWDB!=null&&mWDB.isOpen()){
            mWDB.close();
            mWDB=null;
        }
    }

    //创建数据库,执行建表语句
    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        String sql="create table if not exists "+table_name+" ("+
                " ID varchar not null," +
                " name varchar not null,"+
                " password varchar not null," +
                " phone varchar not null," +
                " classroom varchar not null);";

        sqLiteDatabase.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        //onCreate(sqLiteDatabase);
    }

    //添加语句
    public long insert(User user){
        ContentValues values=new ContentValues();
        values.put("name",user.nameis);
        values.put("ID",user.ID);
        values.put("password",user.password);
        values.put("phone",user.phone);
        values.put("classroom",user.classroom);
        //values.put("student",user.getStudent());

        return mWDB.insert(table_name,null,values);
    }
}

 

标签:name,App,private,String,import,打卡,password,android,页面
From: https://www.cnblogs.com/cinan/p/17232173.html

相关文章

  • 简单的记事本app1
    (因为时间原因功能尚不全面,之后会继续更新)放一下目录结构,大体会用到这些——(有对应activity.xxx.xml的,创建的时候要创建activity,不要单独创建java文件)   ——这一篇......
  • 每日打卡
    Androidstdio连接数据库:(1)AndroidManifest.xml文件:<?xmlversion="1.0"encoding="utf-8"?><manifestxmlns:android="http://schemas.android.com/apk/res/android"......
  • 大型买返商城交易返利抢购小程序app定制开发h5源码二开搭建
    一.注册和登录1.可以通过手机验证码注册,也可以账号密码登录2.登录或者注册,须同意商家的协议或者规则二.主页面1.主界面分四个部分,分别是广告窗口,菜单栏,活动页面,商品展示三.......
  • 免登录积分商城兑换热门排行会员小程序app源码包售后二开
    一首页1.商品分类;根据商品的分类,可以更快找出心仪的商品2.广告窗口;广告轮播3.活动页面;显示当前的活动,活动时间活动内容,快捷进入活动页面4.商品展示;展示现有商品二疯狂排行1.......
  • [ABC245E] Wrapping Chocolate题解
    听说没人写,那就来一发。这种偏序问题大概率是要排个序的。将盒子和巧克力视为一个东西,\(c\)视为\(a\),\(d\)视为\(b\),放在一起以\(a\)为第一关键字,\(b\)为第二关键......
  • MT6737 android 7.0 竖屏横用后u盘以及下载app无法打开
    问题描述:MT6737android7.0竖屏横用后u盘以及下载app无法打开问题的原因:下载APP的布局不支持横屏显示修改方法:diff--gita/frameworks/base/packages/DocumentsUI/Androi......
  • CF1804F Approximate Diameter 题解
    前言在学校机房被学长推荐了这道题,听完正解后惊为天人...简化版题面给定一张无向连通图,定义直径\(d=\max(dis(i,j))\quad(i,j\inN)\),其中\(dis(i,j)\)指的是\(......
  • 【Android 逆向】【攻防世界】app2
    1.手机安装apk,随便点击,进入到第二个页面就停了2.jadx打开apk,发现一共有三个activity,其中第三个activity:FileDataActivity里面有东西publicclassFileDataActivitye......
  • 【Android 逆向】【攻防世界】app1
    1.apk安装到手机,老套路了2.jadx打开this.btn.setOnClickListener(newView.OnClickListener(){//fromclass:com.example.yaphetshan.tencentgreat.MainA......
  • uni-app:播放音频mp3(hbuilderx 3.7.3)
    一,官方文档地址https://uniapp.dcloud.net.cn/api/media/audio-context.html#createinneraudiocontext说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnbl......