• Android实现自动登录和记住密码


    效果图:

    在勾选自动登录后下次打开软件会直接跳过登录界面

    代码:

     protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_go);//即activity_login.xml
            findViews();
            if(sp.getBoolean("ISCHECK", false))
            {
                //设置默认是记录密码状态
                rem_pw.setChecked(true);
                etusername.setText(sp.getString("USER_NAME", ""));
    
                //判断自动登陆多选框状态
                if(sp.getBoolean("AUTO_ISCHECK", false))
                {
                    //设置默认是自动登录状态
                    auto_login.setChecked(true);
                    //跳转界面
                    Intent intent = new Intent(GoActivity.this,IndexActivity.class);
                    GoActivity.this.startActivity(intent);
    
                }
            }
            login.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String username = etusername.getText().toString();
                    System.out.println(username);
                    Log.i("TAG",username);
                    UserService uService=new UserService(GoActivity.this);
                    boolean flag=uService.login(username);
    
                    if(flag){
                        Log.i("TAG","登录成功");
                        Toast.makeText(GoActivity.this, "登录成功", Toast.LENGTH_LONG).show();
                        if(rem_pw.isChecked())
                        {
                            //记住用户名、密码、
                            SharedPreferences.Editor editor = sp.edit();
                            editor.putString("USER_NAME", username);
                            editor.commit();
                        }
                        Intent intent = new Intent(GoActivity.this,ListActivity.class);//页面跳转
                        startActivity(intent);
                    }else{
                        Log.i("TAG","登录失败");
                        Toast.makeText(GoActivity.this, "登录失败", Toast.LENGTH_LONG).show();
                    }
    
                }
            });
            register.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
    
                    Intent intent=new Intent(GoActivity.this,RegisterActivity.class);
                    startActivity(intent);
    
                }
            });
            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();
                    }
                }
            });
        }
  • 相关阅读:
    多线程2.md
    Python-多线程.md
    Python-Log-note.md
    记账本开发记录——第四天(2020.1.21)
    记账本开发记录——第三天(2020.1.20)
    记账本开发记录——第二天(2020.1.19)
    《构建之法——现代软件工程》读书笔记(一)
    记账本开发记录——第一天(2020.1.18)
    JAVA分级测试——选课系统(补发)
    转专业后补修C语言的一些体会(4)
  • 原文地址:https://www.cnblogs.com/1305536110-dym/p/14903760.html
Copyright © 2020-2023  润新知