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