Android Studio SharedPreferences复选框未正确保存

1zmg4dgp  于 2022-11-16  发布在  Android
关注(0)|答案(2)|浏览(176)

我用一个复选框进行了一个简单的登录。我的sharedpreferences xml文件在第一次登录时不包含布尔语句,所以复选框是未选中的。对于所有用户的第一次登录,复选框的值不能正确反映。我必须与复选框交互(单击选中/取消选中)才能使其工作,但在那之后,后续的尝试工作正常。我错过了什么?

主要活动.java

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    EditText lUsername = (EditText) findViewById(R.id.lUsername);
    EditText lPassword = (EditText) findViewById(R.id.lPassword);
    Button btnSignin = (Button) findViewById(R.id.btnSignin);
    CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox);

    btnSignin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick (View v) {

            String username = lUsername.getText().toString();
            String password = lPassword.getText().toString();

            SharedPreferences preferences = getSharedPreferences("MYPREFS", MODE_PRIVATE);                

            String details = preferences.getString(username + password + "data", "invalid");
            SharedPreferences.Editor editor = preferences.edit();

            if (details == "invalid") {
               Toast.makeText(MainActivity.this, "Invalid credentials", Toast.LENGTH_SHORT).show();
            } else {
                checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                        if(checkBox.isChecked()) {
                            editor.putBoolean("checked", true);
                        }else{
                            editor.putBoolean("checked", false);
                        }
                        editor.commit();
                    }
                });
                Intent displayScreen = new Intent(MainActivity.this, activity2.class);
                startActivity(displayScreen);
            }
   }
});

显示屏幕.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.DisplayScreen);

    SharedPreferences preferences = getSharedPreferences("MYPREFS", MODE_PRIVATE);

    String username = preferences.getString("username", "invalid");

    if(preferences.contains("checked") && preferences.getBoolean("checked",false) == true) {
        String data = preferences.getString("data", "Hello senior " + username + "!");
        TextView displayInfo = (TextView) findViewById(R.id.helloDisplay);
        displayInfo.setText(data);
    }else {
        String data = preferences.getString("data", "Hello " + username + "!");
        TextView displayInfo = (TextView) findViewById(R.id.helloDisplay);
        displayInfo.setText(data);
    }
}
gcxthw6b

gcxthw6b1#

这是因为你已经使用了checkBox.setOnCheckedChangeListener(),只有当你与复选框交互时才会触发。如果你想在sharedPreferences中保存一些默认值,在你做任何事情之前先把它保存起来,还要声明sharedPreferences和其他东西在你的按钮clickListener之外,而不是在里面。

Button btnSignin = (Button) findViewById(R.id.btnSignin);
CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox);

SharedPreferences preferences = getSharedPreferences("MYPREFS", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();   
editor.putBoolean("checked", false);    //for default case

checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    if(checkBox.isChecked()) {
                        editor.putBoolean("checked", true);
                    }else{
                        editor.putBoolean("checked", false);
                    }
                    editor.commit();
                }
            });

btnSignin.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick (View v) {
            String username = lUsername.getText().toString();
            String password = lPassword.getText().toString();
            String details = preferences.getString(username + password + "data", "invalid");
            if (details == "invalid") {
               Toast.makeText(MainActivity.this, "Invalid credentials", Toast.LENGTH_SHORT).show();
            } else {
                Intent displayScreen = new Intent(MainActivity.this, activity2.class);
            startActivity(displayScreen);
            }
        }
    });
m0rkklqb

m0rkklqb2#

您正在调用startActivity(displayScreen)btnSignin.setOnClickListener内部调用checkBox.setOnCheckedChangeListener。因此,您不会在第一次时获得复选框更改。但如果您导航回MainActivity,您将获得复选框更改,因为checkBox.setOnChecked ChangeListener已设置。
这里有一种方法可以使用,你可以随时调用checkBox.isChecked()方法来检查复选框是否被选中;在您的情况下,最好调用此方法,而不是设置checkBox.setOnCheckedChangeListener。因此,您的代码将变为如下所示:

@Override
protected void onCreate(Bundle savedInstanceState) {

    ...

    btnSignin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick (View v) {

            ...

            if (details == "invalid") {
               ...
            } else {
                if(checkBox.isChecked()) {
                    editor.putBoolean("checked", true);
                } else {
                    editor.putBoolean("checked", false);
                }
                editor.commit();
               ...
            }
        }
    });
}

为了简单起见,您甚至可以这样做:

@Override
protected void onCreate(Bundle savedInstanceState) {

    ...

    btnSignin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick (View v) {

            ...

            if (details == "invalid") {
               ...
            } else {
                editor.putBoolean("checked", checkbox.isChecked());
                editor.commit();
               ...
            }
        }
    });
}

这使代码更易读。

相关问题