每日总结
今天开始老师给我们布置了第一个个人作业,是对假期的体温上报App的升级版,在这次的目标中,老师要求加入了登录注册功能,在注册的时候输入ID,用户,手机号码等,其中多出了比较重要的一项就是第一次注册后以后不用每次登录,也就是说这次的app有保存登录的功能。对任务进行分解,注册登录功能只需要使用数据库保存数据就行了,对于保存登录功能就明天在解决,今天完成用户登录注册的功能。
public class ZHUCE extends AppCompatActivity {
private DataBase databaseHelper; //用于创建帮助器对象
private SQLiteDatabase db; //用于创建数据库对象
private static final String name = "JZ.db"; //数据库名称
private static final int version = 1; //数据库版本
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_z_h_u_c_e);
}
public void zhuce(View view){
EditText EditZCid=(EditText)findViewById(R.id.ZCID);
EditText EditPA1=(EditText)findViewById(R.id.ZCP1);
EditText EditPA2=(EditText)findViewById(R.id.ZCP2);
EditText ZCName=(EditText)findViewById(R.id.ZCName);
EditText ZCNPhone=(EditText)findViewById(R.id.ZCPhone);
EditText ZCBJ=(EditText)findViewById(R.id.ZCBJ);
String ZCid= EditZCid.getText().toString();
String zcname=ZCName.getText().toString();
String zcphone=ZCNPhone.getText().toString();
String zcbj=ZCBJ.getText().toString();
List<YHBean> list=new ArrayList<YHBean>();
String ZCP1=EditPA1.getText().toString();
String ZCP2=EditPA2.getText().toString();
databaseHelper = new DataBase(this, name, null, version);
DataBase moh=new DataBase(this,"JZ.db", null, 1);
SQLiteDatabase db = moh.getReadableDatabase(); // 以只读的方式打开数据库
String sql = "select * from yonghu;";
Cursor cursor = db.rawQuery(sql, null);
while (cursor.moveToNext()) {
String id = cursor.getString(cursor.getColumnIndex("xuehao"));
String pass = cursor.getString(cursor.getColumnIndex("password"));
YHBean person = new YHBean(id,pass);
list.add(person); // 添加到数组
}
cursor.close();
int biaoji=0;
for(int i=0;i<list.size();i++){
if(ZCid.equals(list.get(i).getID())){
biaoji=1;
break;
}
}
if(biaoji==1){
Toast.makeText(getApplicationContext(),"该ID已被注册", Toast.LENGTH_SHORT).show();
}
else if(!ZCP1.equals(ZCP2)){
Toast.makeText(getApplicationContext(),"两次密码输入不一致", Toast.LENGTH_SHORT).show();
}
else {
String bbb=zcbj;
db.execSQL("insert into yonghu(xuehao, name,phone,banji,password) values(?, ?,?,?,?)", new Object[]{ZCid,zcname,zcphone,bbb,ZCP1});
Toast.makeText(getApplicationContext(),"注册成功", Toast.LENGTH_SHORT).show();
sql = "select * from ban where banji=\""+zcbj+"\";";
cursor = db.rawQuery(sql, null);
List<BanBean> list2= new ArrayList<>();
while (cursor.moveToNext()) {
String id = cursor.getString(cursor.getColumnIndex("banji"));
String pass = cursor.getString(cursor.getColumnIndex("renshu"));
BanBean person = new BanBean(id,pass);
list2.add(person); // 添加到数组
}
cursor.close();
if(list2.size()==0){
db.execSQL("insert into ban(renshu, banji) values(?, ?)", new Object[]{"1",bbb});
}
else {
int num=0;
for(int i=0;i<list2.size();i++){
if(zcbj.equals(list2.get(i).getBanming())){
num=Integer.parseInt(list2.get(i).getRenshu());
break;
}
}
db.execSQL("update ban set renshu= ? where banji=?",new Object[]{num+1,zcbj});
}
finish();
}
}
}
使用这部分代码即可完成用户的注册,同时会和已经注册的用户进行对比,如果ID已经被注册过会给出用户以提示。