这是DBHelper.java
package cn.itcast.userregist;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import static android.R.attr.tag;
/**
* Created by Ohlaxinyu on 2021/10/30.
*/
public class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context) {
super(context, "ac.db", null, 1);
Log.v("提示","数据库正在创建");
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
//要使用游标适配器,SQLite表格必须包含一栏“_id”
db.execSQL( "create table shop(_id integer primary key autoincrement,name VARCHAR(64), life VARCHAR(64),name VARCHAR(64),gongji VARCHAR(64),sudu VARCHAR(64))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
这是MainActivity.java
package cn.itcast.userregist;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import pl.com.salsoft.sqlitestudioremote.SQLiteStudioService;
public class MainActivity extends AppCompatActivity {
private EditText et_password;
private Button btn_send;
private EditText et_name;
private CheckBox rb_2;
private SharedPreferences sharedPreferences;
private SimpleCursorAdapter adapter;// 简单的游标适配器
private DBHelper dbHelper;
private SQLiteDatabase database; //用于创建数据库对象
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_name = (EditText) findViewById(R.id.et_name);
et_password = (EditText) findViewById(R.id.et_password);
btn_send = (Button) findViewById(R.id.btn_send);
rb_2=(CheckBox) findViewById(R.id.rb_2);
btn_send = (Button) findViewById(R.id.btn_send);
dbHelper = new DBHelper(this.getApplicationContext());
//下面这句创建数据库后添加后 真机上就会显示app has stoped
database = dbHelper.getReadableDatabase();
//点击开始游戏按钮进行数据传递
btn_send.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
passDate();
}
});
et_name.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String res = s.toString();
String res2 = "";
String res3="";
SharedPreferences sharedPreferences = MainActivity.this.getSharedPreferences("Myshard",MainActivity.this.MODE_PRIVATE);
// 获取数据因为不需要对数据进行更改,所以不需要Editor对象,可以用sharedPreferences进行获取
res2 = sharedPreferences.getString("usename","duibi");
if(res.equals(res2)){
res3 = sharedPreferences.getString("usepas","duibi");
et_password.setText(res3);
Toast toast = Toast.makeText(getBaseContext(),"检测到用户:"+res2,Toast.LENGTH_LONG);
toast.show();
}else {
et_password.setText("");
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
//传递数据
public void passDate() {
//创建Intent对象,启动Activity02
Intent intent = new Intent(this, ShowActivity.class);
//将数据存入Intent对象
intent.putExtra("name", et_name.getText().toString().trim());
intent.putExtra("password", et_password.getText().toString().trim());
startActivity(intent);
if(rb_2.isChecked()){
SharedPreferences sharedPreferences = MainActivity.this.getSharedPreferences("Myshard",MainActivity.this.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("usepas",et_password.getText().toString());
editor.putString("usename",et_name.getText().toString());
editor.commit();
}else {
et_password.setText("");
SharedPreferences sharedPreferences = MainActivity.this.getSharedPreferences("Myshard",MainActivity.this.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("usepas","no");
editor.putString("usename","no");
editor.commit();
}
et_password.setText("");
et_name.setText("");
}
public void listenInput(){
};
}