原生登录注册(MVP框架实现)+sharepreferces记住密码

布局我就不展示了,主要代码如下:
第一步:登录和注册的契约类:

 /**
     * p层
     */
    abstract class LoginPresenter{
        public abstract void register(HashMap<String,String> params);
        public abstract void login(HashMap<String,String> params);
    }
    /**
     * m层
     */
    interface LoginModel{
        void register(HashMap<String,String> params, model.LoginModel.ModelCallBack modelCallBack);
        void login(HashMap<String,String> params, model.LoginModel.ModelCallBack modelCallBack);
    }
    /**
     * v层
     */
    interface LoginView{
        void MobileError(String error);
        void success(String result);
        void Fail(String msg);
    }

第二步:model层的请求数据(略)p层的正则验证如下:

//判断手机号
        String phone = params.get("phone");
        if(!ValidatorUtil.isMobile(phone)){
            if(loginView != null){
                loginView.MobileError("手机号不合法");
            }
            return;
        }

第三步:view层,执行成功的回调方法

LoginBean loginBean = new Gson().fromJson(result,LoginBean.class);
        Toast.makeText(MainActivity.this,loginBean.getMessage(),Toast.LENGTH_SHORT).show();
        if(loginBean.getMessage().equals("登录成功")){
            Intent intent = new Intent(MainActivity.this,Main2Activity.class);
            startActivity(intent);
        }

第四步:view层的记住密码

//点击登录监听
        login_but.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //获取输入框输入的值
                String login_phone = login_edit_phone.getText().toString();
                String login_pwd = login_edit_pwd.getText().toString();
                HashMap<String,String> params = new HashMap<>();
                params.put("phone",login_phone);
                params.put("pwd",login_pwd);

                if(presenter != null){
                    presenter.login(params);
                }
				//点击记住密码
                if(login_remeber.isChecked()){

                        editor.putBoolean("ischecked",true);
                        editor.putString("phone",login_phone);
                        editor.putString("pwd",login_pwd);
                        editor.commit();
                }
            }
        });

记住密码处理

        getSupportActionBar().hide();//隐藏标题栏

          sharedPreferences = getSharedPreferences("user",MODE_PRIVATE);
          editor = sharedPreferences.edit();
        boolean ischecked = sharedPreferences.getBoolean("ischecked", false);
        if(ischecked){
            String phone = sharedPreferences.getString("phone", null);
            String pwd = sharedPreferences.getString("pwd", null);
            login_remeber.setChecked(true);
            login_edit_phone.setText(phone);
            login_edit_pwd.setText(pwd);

猜你喜欢

转载自blog.csdn.net/weixin_43808025/article/details/86549976