记住密码,自动登录

sharedPreferences=getSharedPreferences("User", MODE_PRIVATE);
        editor=sharedPreferences.edit();
      
        //实现联动效果
        cbLogin.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                if(isChecked){
                    cbPwd.setChecked(true);
                }else{
                    cbPwd.setChecked(false);
                }
            }
        });
       
        //得到我们的存入的状态值,如果记住密码或自动登录已勾选,那么状态值为true,那就进入一以下的判断,如果未勾选,反之
        boolean Checkedone=sharedPreferences.getBoolean("Checkedone", false);
        boolean Checkedtwo=sharedPreferences.getBoolean("Checkedtwo", false);
      
        //进入当前判断,从sharedPreferences里取出账号、密码为输入框设置,记住密码的复选框为勾选状态
        if(Checkedone){//记住密码
            String name=sharedPreferences.getString("name", null);
            String pass=sharedPreferences.getString("pass", null);
            ed1.setText(name);
            ed2.setText(pass);
            cbPwd.setChecked(true);
        }
        if(Checkedtwo){//记住自动登陆
            cbLogin.setChecked(true);
            //直接跳转到下一页面
            Intent it=new Intent(MainActivity.this,TwoActivity.class);
            startActivity(it);
            finish();
        }
    }
    
    //设置点击监听
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        
        switch (v.getId()) {
        case R.id.but:
            //判断当前自动登录的勾选状态
            if(cbLogin.isChecked()){
                String name=ed1.getText().toString();
                String pass=ed2.getText().toString();
                
                editor.putString("name", name);//保存用户名
                editor.putString("pass", pass);//保存密码
                editor.putBoolean("Checkedtwo", true);//保存状态值
                
                editor.commit();
            }
            /*
             * 判断如果复选框为true时,进入当前判断,首先获取当前的账号和密码,然后在进行保存,我们还需要
             * 存入一个状态值
             * 记住密码
             */
            if(cbPwd.isChecked()){
                String pass=ed2.getText().toString();
                
                editor.putString("pass", pass);//保存密码
                editor.putBoolean("Checkedone", true);//保存状态值
                
                editor.commit();
            }

            //跳转到下一页面
            Intent it=new Intent(MainActivity.this,TwoActivity.class);
            startActivity(it);
            //关闭当前的activity
            finish();
            break;

        default:
            break;
        }
    }



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值