记住密码、自动登录

1、MainActivity

public class MainActivity<T> extends BaseActivity implements IView<T>, View.OnClickListener {

    private String url="http://www.xieast.com/api/user/login.php";
    private EditText tel;
    private EditText pwd;
    private CheckBox jizhumima;
    private CheckBox zidongdenglu;
    private Button tj;
    private Button third;
    private MyPresenter myPresenter;
    private SharedPreferences preferences;
    private SharedPreferences.Editor edit;
    private String strTel;
    private String strPwd;

    @Override
    protected int getLayout() {
        return R.layout.activity_main;
    }
    @Override
    protected void initView() {
        tel = findViewById(R.id.tel);
        pwd = findViewById(R.id.pwd);
        jizhumima = findViewById(R.id.jizhumima);
        zidongdenglu = findViewById(R.id.zidongdenglu);
        tj = findViewById(R.id.tj);
        third = findViewById(R.id.third);
    }

    @Override
    protected void setClick() {
        jizhumima.setOnClickListener(this);
        zidongdenglu.setOnClickListener(this);
        tj.setOnClickListener(this);
        third.setOnClickListener(this);
    }

    @Override
    protected void progressLogic() {
        preferences = getSharedPreferences("zjh",MODE_PRIVATE);
        edit = preferences.edit();
        myPresenter = new MyPresenter(this);
        boolean isjizhu = preferences.getBoolean("isjizhu", false);
        boolean iszidong = preferences.getBoolean("iszidong", false);
        jizhumima.setChecked(isjizhu);
        zidongdenglu.setChecked(iszidong);
        String name=preferences.getString("name",null);
        String pass=preferences.getString("pass",null);

        if(isjizhu){
            tel.setText(name);
            pwd.setText(pass);

        }

        if(iszidong){
            Intent intent = new Intent(MainActivity.this,Main2Activity.class);
            startActivity(intent);
            finish();
            jizhumima.setChecked(true);
        }
        
    }

    //点击事件
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.jizhumima:
                if(!jizhumima.isChecked()){
                    zidongdenglu.setChecked(false);
                }
                break;
            case R.id.zidongdenglu:
                if(zidongdenglu.isChecked()){
                    jizhumima.setChecked(true);
                }
                break;
            case R.id.tj:
                strTel = tel.getText().toString();
                strPwd = pwd.getText().toString();
                boolean b = isCellphone(strTel);
                if (strTel.isEmpty()|| strPwd.isEmpty()){
                    Toast.makeText(this, "手机号或密码不能为空", Toast.LENGTH_SHORT).show();
                }else{
                    url="http://www.xieast.com/api/user/login.php?username="+ strTel +"&password="+ strPwd;
                    if(!b){
                        Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    myPresenter.startRequest(url,1);
                }
                break;
            case R.id.third:

                break;
        }
    }

    //正则
    public static boolean isCellphone(String str) {
        Pattern pattern = Pattern.compile("^((13[0-9])|(14[5,7,9])|(15[^4])|(18[0-9])|(17[0,1,3,5,6,7,8]))\\d{8}$");
        Matcher matcher = pattern.matcher(str);
        return matcher.matches();
    }

IView
    @Override
    public void setSuccessData(T data) {
        Info info = (Info) data;
        if(info.getCode()==100){
            if(jizhumima.isChecked()){
                edit.putBoolean("isjizhu",true);
                edit.putString("name",strTel);
                edit.putString("pass",strPwd);
            }else{
                edit.putBoolean("isjizhu",false);
                edit.putBoolean("iszidong",false);

            }
            if(zidongdenglu.isChecked()){
                edit.putBoolean("isjizhu",true);
                edit.putBoolean("iszidong",true);
            }else{
                edit.putBoolean("iszidong",false);
            }
            edit.commit();
            Toast.makeText(this, info.getMsg(), Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(MainActivity.this,Main2Activity.class);
            startActivity(intent);
        }else{
            Toast.makeText(this, info.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void setErrorData(T errorData) {
        Toast.makeText(this, errorData+"", Toast.LENGTH_SHORT).show();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值