团队博客-自动登录

为了改善用户的使用体验

使得登录不再需要一次又一次的输入用户密码

我们使用了sharehelper存储实现了记住账号密码,并且自动登录的功能

sp = this.getSharedPreferences( "userInfo", Context.MODE_PRIVATE );
            username = findViewById( R.id.username );
            password = findViewById( R.id.password );
            login = findViewById( R.id.btn_login );
            register = findViewById( R.id.btn_register );
            rem_pw = findViewById( R.id.checkbox_text );
            auto_login = findViewById( R.id.checkbox_zhanghao );
        /*Map<String, String> map=User.Get(this);
        if(map!=null){
            username.setText(map.get("user"));
            password.setText(map.get("key"));
        }*/

        //判断记住密码多选框的状态
        if(sp.getBoolean("ISCHECK", true))
        {
            //设置默认是记录密码状态
            rem_pw.setChecked(true);
            username.setText(sp.getString("USER_NAME", ""));
            password.setText(sp.getString("PASSWORD", ""));
            //判断自动登陆多选框状态
            if(sp.getBoolean("AUTO_ISCHECK", true))
            {
                //设置默认是自动登录状态
                auto_login.setChecked(true);
                //跳转界面
                Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                LoginActivity.this.startActivity(intent);

            }
        }

        //登录事件
            login.setOnClickListener( new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String name = username.getText().toString();
                    System.out.println( name );
                    String pass = password.getText().toString();
                    System.out.println( pass );

                    Log.i( "TAG", name + "_" + pass );
                    UserService uService = new UserService( LoginActivity.this );
                    boolean flag = uService.login( name, pass );

                    if (flag) {
                        Log.i( "TAG", "登录成功" );
                        Toast.makeText( LoginActivity.this, "登录成功", Toast.LENGTH_LONG ).show();
                        //登录成功和记住密码框为选中状态才保存用户信息
                        if(rem_pw.isChecked())
                        {
                            //记住用户名、密码、
                            SharedPreferences.Editor editor = sp.edit();
                            editor.putString("USER_NAME", name);
                            editor.putString("PASSWORD", pass);
                            editor.commit();
                        }
                        //跳转界面
                        Intent intent = new Intent( LoginActivity.this, MainActivity.class );
                        startActivity( intent );
                    }
                    else {
                        Log.i( "TAG", "登录失败" );
                        Toast.makeText( LoginActivity.this, "登录失败", Toast.LENGTH_LONG ).show();
                    }
                }
            } );

        rem_pw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
                if (rem_pw.isChecked()) {

                    System.out.println("记住密码已选中");
                    sp.edit().putBoolean("ISCHECK", true).commit();

                }else {

                    System.out.println("记住密码没有选中");
                    sp.edit().putBoolean("ISCHECK", false).commit();

                }

            }
        });

        //监听自动登录多选框事件
        auto_login.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
                if (auto_login.isChecked()) {
                    System.out.println("自动登录已选中");
                    sp.edit().putBoolean("AUTO_ISCHECK", true).commit();

                } else {
                    System.out.println("自动登录没有选中");
                    sp.edit().putBoolean("AUTO_ISCHECK", false).commit();
                }
            }
        });
原文地址:https://www.cnblogs.com/dty602511/p/14762515.html