gpt4 book ai didi

java - 带有 5 个复选框的进度条

转载 作者:行者123 更新时间:2023-11-30 06:24:07 25 4
gpt4 key购买 nike

我需要进度条来加载选中的复选框数量的百分比。

我尝试将它用于 5 个复选框:

“progressbar.setProgress (20)”(如果已选中),如果不是“progressbar.setProgress (-20)”

即使选中了 5 个复选框,进度条也仅加载“20%”。有人可以帮助我吗?

公共(public)类 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();
}
}
});

}

}

最佳答案

您的问题是您总是将其设置为 20 或 -20,而不是从当前值更新它。使用

progressBar.setProgress(progressBar.getProgress()+20);

progressBar.setProgress(progressBar.getProgress()-20);

关于java - 带有 5 个复选框的进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47564082/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com