zoukankan      html  css  js  c++  java
  • 团队博客(五)

    5.7

    为了改善用户的使用体验

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

    我们使用了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();
                    }
                }
            });
  • 相关阅读:
    09-2:跳台阶
    09:菲波那切数列
    08:旋转数组的最小值
    07:用两个栈实现队列
    06:重建二叉树
    05:从尾到头打印链表
    04:替换字符
    centos7安装Jenkins更改默认端口并配置Ldap服务器进行用户认证
    Jira配置openLdap服务器进行用户认证
    定时自动从FTP服务器取数据脚本
  • 原文地址:https://www.cnblogs.com/wangdayang/p/14762551.html
Copyright © 2011-2022 走看看