Java 带有5个复选框的进度条

Java 带有5个复选框的进度条,java,android-studio,checkbox,Java,Android Studio,Checkbox,我需要progressBar加载选中复选框的百分比 我尝试将其用于5个复选框: “progressbar.setProgress(20)”如果选中,如果不是“progressbar.setProgress(-20)” 即使选中了5个复选框,progressBar也只能加载“20%”。有人能帮我吗 公共类MainActivity扩展了AppCompatActivity{ @Override protected void onCreate(Bundle savedInstanceState) {

我需要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();
            }
        }
    });

}

}

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

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


欢迎来到堆栈溢出!当问题清晰、具体,并且包含一个完整的答案时,最有可能得到很好的帮助。请发布一些您遇到问题的实际代码。就绪。谢谢@bnaecker:D
progressBar.setProgress(progressBar.getProgress()-20);