Android登录+记住密码+显示或隐藏密码

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.bind(this);

    mainPresenter = new MainPresenter();
    mainPresenter.setView(this);

用sp保存

        sp = this.getSharedPreferences("login", Context.MODE_PRIVATE);

        if (sp.getBoolean("check",true)){
            loginPhone.setText(sp.getString("phone",""));
            loginPwd.setText(sp.getString("pwd",""));
            loginCheckBox.setChecked(sp.getBoolean("check",true));

        }else {
            loginPhone.setText(sp.getString("phone",""));
        }

设置显示或隐藏密码
默认隐藏

    loginSee.setChecked(false);
    loginSee.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (loginSee.isChecked()){
                loginPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            }else {
                loginPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
            }
        }
    });

记住密码的选框

    loginCheckBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (loginCheckBox.isChecked()){
                loginCheckBox.setChecked(true);
            }else {
                loginCheckBox.setChecked(false);
            }
        }
    });

登录按钮的点击事件

    loginBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            phone = loginPhone.getText().toString();
            pwd = loginPwd.getText().toString();

            if (phone.isEmpty() || pwd.isEmpty()){
                Toast.makeText(MainActivity.this, "输入内容不能为空", Toast.LENGTH_SHORT).show();
            }else {
                map.put("phone",phone);
                map.put("pwd",pwd);
                mainPresenter.login(map);
            }
        }
    });

去注册的点击事件

    loginRegister.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(MainActivity.this,RegisterActivity.class));
            finish();
        }
    });

在成功的方法里面存入用户名和密码

@Override
public void onSuccess(LoginBean loginBean) {
    if (loginBean.getStatus().equals("0000")){
        editor = sp.edit();
        if (loginCheckBox.isChecked()){
            editor.putBoolean("check",true);
            editor.putString("phone",phone);
            editor.putString("pwd",pwd);
        }else {
            editor.putBoolean("check",false);
            editor.putString("phone",phone);
        }
        editor.commit();
        Toast.makeText(this, "欢迎回来", Toast.LENGTH_SHORT).show();
        startActivity(new Intent(this,ShowActivity.class));
        finish();
    }else if (loginBean.getStatus().equals("1001")){
        Toast.makeText(this, ""+loginBean.getMessage(), Toast.LENGTH_SHORT).show();
    }
}

失败的方法

@Override
public void onError(String err) {

}

你可能感兴趣的:(Android登录+记住密码+显示或隐藏密码)