计数复选框的进度条

时间:2017-11-25 18:24:03

标签: java android checkbox progress-bar

我需要每次点击复选框以增加progressBar中的百分比。

示例:我有两个复选框,每个复选框对应progressBar中的50%。

我尝试了几个代码,但都没有用,我是java的初学者

有人能帮助我吗?请。

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        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);         

        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);
                    editor.apply();
                } else {
                    editor.putBoolean("checkbox1", false);
                    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);
                    editor.apply();
                } else {
                    editor.putBoolean("checkbox2", false);
                    editor.apply();
                }
            }
        });
    }
}

0 个答案:

没有答案