qq_41093527 2021-06-03 11:18 采纳率: 0%
浏览 43

安卓studioSharedPreferences记住密码和自动登录

public class MainActivity extends AppCompatActivity {

    private TextView textView;
    private EditText edtTxtMainUserName;
    private EditText edtTxtMainPassword;
    private CheckBox chkBoxMainauto;
    private CheckBox chkBoxMainPSW;
    private Button btnMainLogin;
    private Button btnMainzhuce;
    private SharedPreferences sp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        sp=getSharedPreferences("Login",MODE_PRIVATE);
         chkBoxMainauto.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
             @Override
             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                 SharedPreferences.Editor editor=sp.edit();
//                 editor.putBoolean("autologin",isChecked);
//                 editor.putBoolean("autologin",isChecked);
                 if (isChecked){
                     chkBoxMainauto.setChecked(true);
                 }
             }
         });
        chkBoxMainPSW.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                SharedPreferences.Editor editor=sp.edit();
                editor.putBoolean("chkName",isChecked);
                editor.putBoolean("chkPSW",isChecked);
                editor.commit();
                if (isChecked){
                    chkBoxMainPSW.setChecked(true);

                }

            }
        });
        btnMainLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String userName=edtTxtMainUserName.getText().toString();
                String password=edtTxtMainPassword.getText().toString();
//                if (chkBoxMainauto.isChecked()){
//                    SharedPreferences.Editor editor=sp.edit();
//                    editor.putBoolean("autologin",true);
//                }else{
//                    SharedPreferences.Editor editor=sp.edit();
//                    editor.putBoolean("autologin",false);
//                }
                if (userName.equals("admin")&&password.equals("123456")){
//                    Toast.makeText(MainActivity.this,"登陆成功",Toast.LENGTH_SHORT).show();
                    SharedPreferences.Editor editor = sp.edit();
                    editor.putString("userName", userName);
                    editor.putString("password",password);
                    editor.commit();
                    Intent intent=new Intent(MainActivity.this,MainActivity2.class);
                    MainActivity.this.startActivity(intent);

                } else{
                    AlertDialog dialog;
                    AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
                    builder.setMessage("账号或密码错误");
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                       dialog.dismiss();
//
                        }
                    });
                    dialog=builder.create();
                    dialog.show();
                }

            }

        });
    }

    @Override
    protected void onStart(){
        super.onStart();
        boolean auto=sp.getBoolean("auto",false);
        chkBoxMainPSW.setChecked(auto);
        boolean chkPSW=sp.getBoolean("chkPSW",false);
        chkBoxMainPSW.setChecked(chkPSW);
        boolean chkName=sp.getBoolean("chkName",false);
        chkBoxMainPSW.setChecked(chkName);
        if (chkName){
            edtTxtMainUserName.setText(sp.getString("userName",null));
        }
        if (chkPSW){
            edtTxtMainPassword.setText(sp.getString("password",null));
        }
    }

    private void initView() {
        textView = (TextView) findViewById(R.id.textView);
        edtTxtMainUserName = (EditText) findViewById(R.id.edtTxtMainUserName);
        edtTxtMainPassword = (EditText) findViewById(R.id.edtTxtMainPassword);
        chkBoxMainauto = (CheckBox) findViewById(R.id.chkBoxMainauto);
        chkBoxMainPSW = (CheckBox) findViewById(R.id.chkBoxMainPSW);
        btnMainLogin = (Button) findViewById(R.id.btnMainLogin);
        btnMainzhuce = (Button) findViewById(R.id.btnMainzhuce);
    }
}
  • 写回答

2条回答 默认 最新

  • qq_41093527 2021-06-03 11:18
    关注

    自动登录功能怎么弄出来呢 有大佬们知道吗

    评论

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址