前段时间学习数据存储,sp,内部,外部文件存储,SqLite数据库存储。学完利空课余时间写了一个实现登录注册的demo
先看一下效果图吧
接下来看代码,布局文件代码太简单了我就不贴出来了
注册代码
package com.example.xiaofeiwa.myqqloginapplication;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class ResterActivity extends AppCompatActivity {
MySqLiteHelper helper = null;
private EditText register_user, register_password;
private Button register_suer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rester);
//创建数据库
helper = new MySqLiteHelper(this, "SAVE_USER.db", null, 1);
SQLiteDatabase database = helper.getReadableDatabase();
initViews();
}
private void initViews() {
register_user = findViewById(R.id.et_register_user);
register_password = findViewById(R.id.et_register_password);
register_suer = findViewById(R.id.btn_register_suer);
//点击确定按钮返回登录界面
register_suer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//trim()去空格
String RegisterUser = register_user.getText().toString().trim();
String RegisterPassword = register_password.getText().toString().trim();
//判断输入的账号密码是否为空,是则提示用户输入正确的
if (RegisterUser.equals("") && RegisterPassword.equals("")) {
Toast.makeText(ResterActivity.this, "请输入账号/密码", Toast.LENGTH_LONG).show();
} else {
//账号密码有效,执行下一步
/**
* 把获取的数据存储到数据库
* 1.insert方法添加数据到数据库
*/
SQLiteDatabase database = helper.getReadableDatabase();
ContentValues values = new ContentValues();
//键值对的形式
values.put("name", RegisterUser);
values.put("password", RegisterPassword);
//添加到user表
database.insert("user", null, values);
Toast.makeText(ResterActivity.this, "数据保存成功", Toast.LENGTH_LONG).show();
//账号密码验证正确,显示意图跳转到登录界面
//Toast.makeText(ResterActivity.this, "注册成功", Toast.LENGTH_LONG).show();
Intent intent;
intent = new Intent(ResterActivity.this, MainActivity.class);
startActivity(intent);
}
// if (RegisterUser.length() != 11){
// Toast.makeText(ResterActivity.this, "请输入正确手机号", Toast.LENGTH_LONG).show();
// }
}
});
}
}
MySqLiteHelper类
package com.example.xiaofeiwa.myqqloginapplication;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;
public class MySqLiteHelper extends SQLiteOpenHelper {
private static final String LAN_USER = "create table user(name text,password text)";
Context context;
public MySqLiteHelper(Context context,String name,SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(LAN_USER);
Toast.makeText(context,"数据库创建成功!",Toast.LENGTH_LONG).show();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
MainActivity
package com.example.xiaofeiwa.myqqloginapplication;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
MySqLiteHelper helper;
private EditText user, password;
private Button login, register;
private String name;
private String psd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//数据库操作
helper = new MySqLiteHelper(this, "SAVE_USER.db", null, 1);
SQLiteDatabase database = helper.getReadableDatabase();
Cursor cursor = database.rawQuery("select * from user", null);
if (cursor.moveToFirst()) {
do {
//从数据库获取到用户名、密码
name = cursor.getString(cursor.getColumnIndex("name"));
psd = cursor.getString(cursor.getColumnIndex("password"));
} while (cursor.moveToNext());
//关闭游标
cursor.close();
}
initViews();
}
private void initViews() {
user = findViewById(R.id.et_user);
password = findViewById(R.id.et_password);
login = findViewById(R.id.btn_login);
register = findViewById(R.id.btn_register);
//注册按钮点击事件
register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, ResterActivity.class);
startActivity(intent);
}
});
//登录按钮点击事件
//验证当前账号、密码是否跟注册报讯在数据库的账号、密码一致,一致则允许登录
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获取输入框文本
String username = user.getText().toString().trim();
String mpassword = password.getText().toString().trim();
//database.query("user",null,null,null,null,null,null);
if (username.equals("") && mpassword.equals("")) {
Toast.makeText(MainActivity.this, "账号/密码不能为空", Toast.LENGTH_LONG).show();
} else {
sureuser(username, mpassword);
}
}
});
}
private void sureuser(String username, String mpassword) {
if (username.equals(name) && mpassword.equals(psd)) {
//账号密码校验正确
// 登陆成功,跳转的用户界面
Intent intent = new Intent(this, UserActivity.class);
startActivity(intent);
//提示
Toast.makeText(MainActivity.this, "登陆成功!", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this, "手机号/密码错误!", Toast.LENGTH_LONG).show();
}
}
}
至于登陆成功的界面就不贴出来了,大家自由发挥