@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_achievements);
    CheckBox twentyCheck = findViewById(R.id.twentyCheck);
    SharedPreferences settings = getSharedPreferences("GAME DATA", Context.MODE_PRIVATE);
    int highScore = settings.getInt("HIGH_SCORE", 0);
    if (highScore >= 20) {
        twentyCheck.isChecked();
    }
}


在此处查看完整代码:High Score

如果高分大于20,我需要如何检查复选框的帮助。

最佳答案

问题是您正在调用isChecked,这取决于检查框是否被选中而返回true或false。您应该改为调用setChecked。

 @Override
 protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_achievements);
     CheckBox twentyCheck = findViewById(R.id.twentyCheck);
     SharedPreferences settings = getSharedPreferences("GAME DATA", Context.MODE_PRIVATE);
     int highScore = settings.getInt("HIGH_SCORE", 0);
     if (highScore >= 20) {
         twentyCheck.setChecked(true);
     }
 }

关于java - 如果高分大于20,则无法选中此复选框,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54751604/

10-12 04:13