使用CheckBox更改可见性 - Android

时间:2014-04-01 06:48:44

标签: android checkbox progress-bar visibility

我想根据选中的ProgressBar更改CheckBoxes的可见性,但是当我实施以下代码时,ProgressBar将永远隐身

ProgressBar progressBar1 = (ProgressBar) findViewById(R.id.progressBar1);
    ProgressBar progressBar2 = (ProgressBar) findViewById(R.id.progressBar2);
    CheckBox checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
    CheckBox checkBox2 = (CheckBox) findViewById(R.id.checkBox2);

    progressBar1.setVisibility(View.GONE);
        progressBar2.setVisibility(View.GONE);

    if (checkBox1.isChecked() && checkBox2.isChecked()) {
        progressBar2.setVisibility(View.VISIBLE);
    } else if (checkBox1.isChecked()) {
        progressBar1.setVisibility(View.VISIBLE);
    }

1 个答案:

答案 0 :(得分:1)

you have to set the visibility code in the setOnClick listener of the checkbox

    checkBox1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(checkBox1.isChecked())
                {
                  progressBar1.setVisibility(View.VISIBLE);
             }
              else
            {
             progressBar1.setVisibility(View.INVISIBLE);
             }
                }

        });

       checkBox2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                 if(checkBox1.isChecked() && checkBox2.isChecked()) {
                            progressBar2.setVisibility(View.VISIBLE);
                        } else {
                            progressBar2.setVisibility(View.INVISIBLE);
                        }
                }

        });