前言
这篇文章主要是实现上示功能。
首先创建数据库(只能创建一次)
然后输入用户名密码,店家注册,将数据插入数据库
点击记住密码能够在下次登录时自动输入用户名密码
最后点击登录能够进入登录界面。
ps:这里有个bug就是得注册两次才能生效,不太理解哪里有问题了,以后知道了再来补…
问题找到了
没关数据库,所以会有点bug:
cursor.close();
database.close();
及时关闭,就可以解决bug了。
主要文件目录
首先我们看看布局文件:
activity_main.xml
就是一个简单的登录界面的布局。
<?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"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="用户名:"
android:textSize="20sp"
/>
<EditText
android:id="@+id/uname"
android:layout_width="281dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="密 码:"
android:textSize="20sp"
/>
<EditText
android:id="@+id/upwd"
android:layout_width="281dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
</LinearLayout>
<CheckBox
android:id="@+id/cb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="记住密码" />
<Button
android:id="@+id/createdb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="创建数据库"/>
<Button
android:id="@+id/register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="注册" />
<Button
android:id="@+id/login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录" />
</LinearLayout>
main.xml
就是一个简单的登录成功界面的布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="欢迎光临!"
android:textSize="50sp"
tools:layout_editor_absoluteX="131dp"
tools:layout_editor_absoluteY="62dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity
内容结构应该比较清晰,按钮绑定各自对应的事件。
稍微解释一下部分代码:
Cursor cursor = database.rawQuery(
"select * from userTb where uname = ?", new String[]{userName});
表明查询语句,查询到的内容在cursor中,其中?的位置用userName这个字符串来表示,简单来说就是让sql语句写起来方便一点的写法。
String String_upwd = cursor.getString(cursor.getColumnIndex("upwd"));
表明获取查询内容中的upwd字段的内容。
package icy.hunter;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity{
private CheckBox cb;
private EditText uname;
private EditText upwd;
private static final String SP_INFO = "myuser";
private static final String USER_ID = "UserId";
private static final String USERPWD = "UserPwd";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.uname = findViewById(R.id.uname);
this.upwd = findViewById(R.id.upwd);
this.cb = findViewById(R.id.cb);
Button bt_create = findViewById(R.id.createdb);
Button bt_register = findViewById(R.id.register);
Button bt_login = findViewById(R.id.login);
// 记录保存数据情况
checkIfRemember();
// 创建数据库
bt_create.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase("data/data/icy.hunter/user.db", null);
String sql = "create table userTb(uid integer primary key autoincrement, uname text, upwd text)";
db.execSQL(sql);
Toast.makeText(MainActivity.this, "创建数据库以及表成功!", Toast.LENGTH_LONG).show();
}
});
//插入数据库
bt_register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase("data/data/icy.hunter/user.db", null);
String userName = uname.getText().toString().trim();
String userPass = upwd.getText().toString().trim();
if(userName.equals("")||userPass.equals("")){
Toast.makeText(MainActivity.this, "用户名或者密码为空,请重新输入!", Toast.LENGTH_LONG).show();
}else{
String sql = "insert into userTb(uname, upwd) values('"+userName+"', '"+userPass+"')";
db.execSQL(sql);
Toast.makeText(MainActivity.this, "注册用户成功!", Toast.LENGTH_LONG).show();
}
}
});
bt_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String userName = uname.getText().toString().trim();
String userPass = upwd.getText().toString().trim();
SQLiteDatabase database = SQLiteDatabase.openDatabase("data/data/icy.hunter/user.db", null,
SQLiteDatabase.OPEN_READONLY);
Cursor cursor = database.rawQuery(
"select * from userTb where uname = ?", new String[]{userName});
int success = 0;
while (cursor.moveToNext()) {
String String_upwd = cursor.getString(cursor.getColumnIndex("upwd"));
if(userPass.equals(String_upwd)){
success = 1;
Toast.makeText(MainActivity.this, userName+"用户登录成功!", Toast.LENGTH_LONG).show();
Intent it = new Intent(MainActivity.this, Main.class);
startActivity(it);
}
}
if(success == 0){
Toast.makeText(MainActivity.this, userName+"用户名或密码错误或用户不存在", Toast.LENGTH_LONG).show();
}
}
});
}
//存数据
public void rememberMe(String uid, String pwd){
SharedPreferences sp = getSharedPreferences(SP_INFO, MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putString(USER_ID, uid);
editor.putString(USERPWD, pwd);
editor.commit();
}
//读数据
public void checkIfRemember(){
SharedPreferences sp = getSharedPreferences(SP_INFO, MODE_PRIVATE);
String uidStr = sp.getString(USER_ID, null);
String pwdStr = sp.getString(USERPWD, null);
if(uidStr != null && pwdStr != null){
uname.setText(uidStr);
upwd.setText(pwdStr);
cb.setChecked(true);
}
}
@Override
protected void onStop() {
super.onStop();
String uidStr = upwd.getText().toString().trim();
String pwdStr = uname.getText().toString().trim();
if(cb.isChecked()){
rememberMe(uidStr, pwdStr);
}
}
}
Main
为了跳转而设定的activity。
package icy.hunter;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class Main extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
结果
然后我们可以看一下数据库文件和密码记录的文件。
首先我们可以重启一下虚拟机,防止生成的文件没刷新出来…
注意要查看文件虚拟机必须要启动,否则是看不到的。
点击右下角
然后进入data/data滑到最下面。
这里就是存数据的地方了。
可以看到我们存的数据库user.db出现了
这里就是记住密码的文件了
点开可以看到:
里面就是我们存储记住密码的键值对。
一些实现的细节还请详细阅读代码。
代码已打包好可0积分放心下载: