判断是否登录注册 成功

2018-08-05  本文已影响0人  Summer_27d1

登录:
通过数据库查询
···

//查询指定账号是否存在 并获取对应的密码

public ArrayList<HashMap<String,String>> selectUserByAccount(String account){
    ArrayList<HashMap<String,String>> arrayList = new ArrayList<>();
    String sql = "select * from user where account = ?";
    Cursor cr = mys.db.rawQuery(sql,new String[]{account});
    //判断获取数据的数量
    if(cr.getCount() == 1){
        cr.moveToFirst();//把游标对象的游标(指针)强制指向第一条数据
        String psw = cr.getString(cr.getColumnIndex("psw"));
        HashMap<String,String> map = new HashMap<>();
        map.put("psw",psw);
        arrayList.add(map);
    }
    return arrayList;
}

···
Acticity中:
···
@Override
public void onClick(View view) {
String account = edAccount.getText().toString();
String psw = edPsw.getText().toString();
ArrayList<HashMap<String,String>> alist = user.selectUserByAccount(account);
if(alist.size() == 0){
Toast.makeText(LogoActivity.this, "该账号不存在", Toast.LENGTH_SHORT).show();
}else {
if(alist.get(0).get("psw").equals(psw)){
SharedPreferences.Editor edit = sp.edit();
edit.putString("account",account);
edit.putString("pwd",psw);
edit.commit();
ps= new ProgressDialog(LogoActivity.this);
ps.setTitle("登录:");
ps.setMessage("正在登录。。。。");
ps.show();
new Thread(){
@Override
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
runOnUiThread(new Runnable() {
@Override
public void run() {
ps.dismiss();
Intent i = new Intent(LogoActivity.this,MainActivity.class);
setResult(111,i);
finish();
Toast.makeText(LogoActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
}
});
}
}.start();

                }else{
                    edPsw.setText("");
                    Toast.makeText(LogoActivity.this, "密码错误", Toast.LENGTH_SHORT).show();
                }

            }
        }
    });

···
注册时 判断账户是否存在
在Tools工具中判断:

//添加数据

public String insertUser(String account,String psw){
    if(  chargeUserIsExist(account) ){
        return "该账号已存在";
    }else{
        String sql = "insert into user (account,psw) values (?,?)";
        mys.db.execSQL(sql,new Object[]{account,psw});
        return "注册成功";
    }


}
//判断用户信息是否存在
public boolean chargeUserIsExist(String account){
    String sql = "select * from user where account = ?";
    Cursor cr = mys.db.rawQuery(sql,new String[]{account});
    //判断获取数据的数量
    if(cr.getCount() == 1){
        return true;//存在 不能注册
    }else{
        return false;//不存在 可以注册
    }
}
上一篇下一篇

猜你喜欢

热点阅读