我需要progressBar来加载检查复选框的百分比。
我尝试将它用于5个复选框:
“progressbar.setProgress(20)”如果已选中,如果不是“progressbar.setProgress(-20)”
即使选中了5个复选框,progressBar也仅加载“20%”。有人可以帮我吗?
公共类MainActivity扩展了AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_main);
final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
final CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox);
final CheckBox checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
final CheckBox checkBox3 = (CheckBox) findViewById(R.id.checkBox3);
final CheckBox checkBox4 = (CheckBox) findViewById(R.id.checkBox4);
final CheckBox checkBox5 = (CheckBox) findViewById(R.id.checkBox5);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
final SharedPreferences.Editor editor = preferences.edit();
if (preferences.contains("checkbox1") && preferences.getBoolean("checkbox1", false) == true) {
checkBox.setChecked(true);
} else {
checkBox.setChecked(false);
}
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (checkBox.isChecked()) {
editor.putBoolean("checkbox1", true);
progressBar.setProgress(20);
editor.apply();
} else {
editor.putBoolean("checkbox1", false);
progressBar.setProgress(-20);
editor.apply();
}
}
});
if (preferences.contains("checkbox2") && preferences.getBoolean("checkbox2", false) == true) {
checkBox2.setChecked(true);
} else {
checkBox2.setChecked(false);
}
checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (checkBox2.isChecked()) {
editor.putBoolean("checkbox2", true);
progressBar.setProgress(20);
editor.apply();
} else {
editor.putBoolean("checkbox2", false);
progressBar.setProgress(-20);
editor.apply();
}
}
});
if (preferences.contains("checkbox3") && preferences.getBoolean("checkbox3", false) == true) {
checkBox3.setChecked(true);
} else {
checkBox3.setChecked(false);
}
checkBox3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (checkBox3.isChecked()) {
editor.putBoolean("checkbox3", true);
progressBar.setProgress(20);
editor.apply();
} else {
editor.putBoolean("checkbox3", false);
progressBar.setProgress(-20);
editor.apply();
}
}
});
if (preferences.contains("checkbox4") && preferences.getBoolean("checkbox4", false) == true) {
checkBox4.setChecked(true);
} else {
checkBox4.setChecked(false);
}
checkBox4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (checkBox4.isChecked()) {
editor.putBoolean("checkbox4", true);
progressBar.setProgress(20);
editor.apply();
} else {
editor.putBoolean("checkbox4", false);
progressBar.setProgress(-20);
editor.apply();
}
}
});
if (preferences.contains("checkbox5") && preferences.getBoolean("checkbox5", false) == true) {
checkBox5.setChecked(true);
} else {
checkBox5.setChecked(false);
}
checkBox5.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (checkBox5.isChecked()) {
editor.putBoolean("checkbox5", true);
progressBar.setProgress(20);
editor.apply();
} else {
editor.putBoolean("checkbox5", false);
progressBar.setProgress(-20);
editor.apply();
}
}
});
}
}
答案 0 :(得分:1)
您的问题是您始终将其设置为20或-20,而不是从当前值更新它。使用
progressBar.setProgress(progressBar.getProgress()+20);
或
progressBar.setProgress(progressBar.getProgress()-20);