Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
If-else语句和可见性-android_Android_If Statement_Visibility - Fatal编程技术网

If-else语句和可见性-android

If-else语句和可见性-android,android,if-statement,visibility,Android,If Statement,Visibility,我的应用程序中有五个复选框和五个进度条,并根据复选框调用了可见性以使进度条可见/消失。那么在可见性方法中使用if-else语句是否有其他选择,因为在这种情况下,五个复选框中有无限的可能性,因此我必须编写无限的if-else语句,以使progressbar在所有可能性中可见/消失 例如-如果选中复选框1和复选框2,则使progressbar2可见 如果选中复选框1和复选框3,则使progressbar2可见 换句话说,如果选中任何一个复选框,我想使Progressbar 1可见,如果选中任何两个复

我的应用程序中有五个复选框和五个进度条,并根据复选框调用了可见性以使进度条可见/消失。那么在可见性方法中使用if-else语句是否有其他选择,因为在这种情况下,五个复选框中有无限的可能性,因此我必须编写无限的if-else语句,以使progressbar在所有可能性中可见/消失

例如-如果选中复选框1和复选框2,则使progressbar2可见 如果选中复选框1和复选框3,则使progressbar2可见

换句话说,如果选中任何一个复选框,我想使Progressbar 1可见,如果选中任何两个复选框,则使Progressbar 2可见,依此类推

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ProgressBar;

public class Progress extends Activity {

    ProgressBar progressBar1;
    ProgressBar progressBar2;

    CheckBox checkBox1;
    CheckBox checkBox2;

    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;
    SharedPreferences setprefsd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_progress);

        setprefsd = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        progressBar1 = (ProgressBar) findViewById(R.id.progressBar1);
        progressBar2 = (ProgressBar) findViewById(R.id.progressBar2);
        checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
        checkBox2 = (CheckBox) findViewById(R.id.checkBox2);

        if (setprefsd.getBoolean("FirstCheckBox", false) == true) {
            checkBox1.setChecked(true);
            updateProgressBars();
        }

        if (setprefsd.getBoolean("SecondCheckBox", false) == true) {
            checkBox2.setChecked(true);
            updateProgressBars();
        }
    }

    public void updateProgressBars() {
        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);
        }
    }
}
简短回答 可以使用三元运算符简化可见性更改(而不是
if
s)

我想这里的方法是计算复选框的数量,然后设置可见性。您只需更改
updateProgressBars()
方法,如下所示:

public void updateProgressBars() {
    int nbCheckboxes = 0;
    if (checkBox1.isChecked())
        nbCheckboxes++;
    if (checkBox2.isChecked())
        nbCheckboxes++;

    progressBar1.setVisibility(nbCheckboxes >= 1 ? View.VISIBLE : View.GONE);
    progressBar2.setVisibility(nbCheckboxes >= 2 ? View.VISIBLE : View.GONE);
}

其他意见 带有布尔表达式的IF语句 您正在使用以下结构:

if (myBooleanExpression == true)
不需要
==true
,因为这里使用的是布尔表达式。改用:

if (myBooleanExpression)
对变量使用数组

如果使用多个具有相同用途的编号变量,则可能需要考虑使用数组。

public class Progress extends Activity {

    ProgressBar[] progressBars;
    CheckBox[] checkBoxes;

    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;
    SharedPreferences setprefsd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_progress);

        setprefsd = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

        progressBars = new ProgressBar[3]; // 3, or whatever number you have
        progressBars[0] = (ProgressBar) findViewById(R.id.progressBar1); 
        progressBars[1] = (ProgressBar) findViewById(R.id.progressBar2); 
        progressBars[2] = (ProgressBar) findViewById(R.id.progressBar3);

        checkBoxes = new CheckBox[3];  // 3 or whatever number you have
        checkBoxes[0] = (CheckBox) findViewById(R.id.checkBox1);
        checkBoxes[1] = (CheckBox) findViewById(R.id.checkBox2);
        checkBoxes[2] = (CheckBox) findViewById(R.id.checkBox3);

        checkBoxes[0].setChecked(setprefsd.getBoolean("FirstCheckBox", false));
        checkBoxes[1].setChecked(setprefsd.getBoolean("SecondCheckBox", false));
        checkBoxes[2].setChecked(setprefsd.getBoolean("ThirdCheckBox", false));

        updateProgressBars();
    }

    public void updateProgressBars() {
        int nbCheckBoxes = 0;
        for (CheckBox cb : checkBoxes) {
            if (cb.isChecked())
                nbCheckBoxes++;
        }

        for (int i = 0; i < progressBars.length; i++) {
            progressBars[i].setVisibility(nbCheckboxes > i ? View.VISIBLE : View.GONE);
        }
    }
}
公共类进度扩展活动{
进度条[]进度条;
复选框[]复选框;
SharedReferences SharedReferences;
SharedReferences.Editor;
SharedReferences setprefsd;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u progress);
setprefsd=PreferenceManager.getDefaultSharedReferences(getApplicationContext());
progressBars=新的ProgressBar[3];//3,或任何您拥有的数字
progressBars[0]=(ProgressBar)findViewById(R.id.progressBar1);
progressBars[1]=(ProgressBar)findViewById(R.id.progressBar2);
progressBars[2]=(ProgressBar)findViewById(R.id.progressBar3);
checkBoxes=新复选框[3];//3或任何您拥有的数字
复选框[0]=(复选框)findViewById(R.id.checkBox1);
复选框[1]=(复选框)findViewById(R.id.checkBox2);
复选框[2]=(复选框)findViewById(R.id.checkBox3);
复选框[0]。setChecked(setprefsd.getBoolean(“FirstCheckBox”,false));
复选框[1]。setChecked(setprefsd.getBoolean(“SecondCheckBox”,false));
复选框[2]。setChecked(setprefsd.getBoolean(“ThirdCheckBox”,false));
updateProgressBars();
}
public void updateProgressBars(){
int nbcheckbox=0;
用于(复选框cb:复选框){
if(cb.isChecked())
nbcheckbox++;
}
for(int i=0;ii?View.VISIBLE:View.GONE);
}
}
}

您有多少个复选框和进度条?您至少提到了3个,但代码中只有2个似乎是完整的代码。