Java Android Studio for 循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41523205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:28:00  来源:igfitidea点击:

Android Studio for loop

javaandroidfor-loop

提问by Jacques Celliers

I Have the following code and want to find out if there is any way i can put the following code into a forloop so that it is not so long as i am doing it a few times in my code

我有以下代码,想知道是否有任何方法可以将以下代码放入for循环中,这样它就不会像我在我的代码中做了几次一样长

 if (Q1o1.equals("null")){
     button1.setVisibility(View.INVISIBLE); 
 }else{
     button1.setVisibility(View.VISIBLE);
     button1.setText(Q1o1);
 }
 if (Q1o2.equals("null")){
     button2.setVisibility(View.INVISIBLE);
 } else{
     button2.setVisibility(View.VISIBLE);
     button2.setText(Q1o2);
 }
 if (Q1o3.equals("null")){
     button3.setVisibility(View.INVISIBLE);
 } else{
     button3.setVisibility(View.VISIBLE);
     button3.setText(Q1o3);
 }
 if (Q1o4.equals("null")){
     button4.setVisibility(View.INVISIBLE);
 } else{
     button4.setVisibility(View.VISIBLE);button4.setText(Q1o4);
 }
 if (Q1o5.equals("null")){
     button5.setVisibility(View.INVISIBLE);
 } else{
     button5.setVisibility(View.VISIBLE);
     button5.setText(Q1o5);
 }
 if (Q1o6.equals("null")){
     button6.setVisibility(View.INVISIBLE);
 } else{
     button6.setVisibility(View.VISIBLE);
     button6.setText(Q1o6);
 }
 if (Q1o7.equals("null")){
     button7.setVisibility(View.INVISIBLE);
 } else{
     button7.setVisibility(View.VISIBLE);
     button7.setText(Q1o7);
 }

Every if statement checks if the relevant variable that needs to be the button text is not null and then either hides it or puts that variable inside the button and shows the button. How can i make a for loop using different variables for each loop?

每个 if 语句都会检查需要作为按钮文本的相关变量是否不为空,然后将其隐藏或将该变量放在按钮内并显示按钮。如何为每个循环使用不同的变量进行 for 循环?

采纳答案by Carlton

Maybe declare arrays like this:

也许像这样声明数组:

String[] stringArray = {*your strings here*};
ButtonView[] buttonArray = {*your buttons here*};

and a loop like this:

和这样的循环:

for(int i = 0; i < stringArray.length; i++){
    String thisString = stringArray[i];
    ButtonView thisButton = buttonArray[i];
    if(thisString.equals(null)){
        thisButton.setVisibility(View.INVISIBLE);
    }else{
        thisButton.setVisibility(View.VISIBLE);
        thisButton.setText(thisString);
    }
}

回答by Jort de Bokx

I would suggest creating an array of the Q1o7's, and an array of the buttons, then using a for loop between 0 and the length of the array, in which you will all the appropriate methods for the selected element in the array

我建议创建一个Q1o7's 数组和一个按钮数组,然后在 0 和数组长度之间使用 for 循环,您将在其中使用数组中所选元素的所有适当方法

Example:

例子:

First create the arrays:

首先创建数组:

Button[] buttonArray = {button1, .., button7}; //Replace the .. by the other buttons
String[] qoValues = {QO1, .., QO7} //As before

Now we create a loop that iterates over these arrays and calls the appropriate functions:

现在我们创建一个循环来遍历这些数组并调用适当的函数:

for (int i = 0; i < qoValues.length; i++) { //May also use buttonArray.length 
    ButtonView currentButton= buttonArray[i]; //get current value to work with
    String currentString = stringArray[i];
    if (currentString .equals("null")) { //Similar If statement construction as before
        currentButton.setVisibility(View.INVISIBLE);
    } else {
        currentButton.setVisibility(View.VISIBLE);
        currentButton.setText(currentString );
    }
}

回答by AouledIssa

All you have to do is to define two Arralists where one is for buttons and the other is for variables :

您所要做的就是定义两个 Arralists,其中一个用于按钮,另一个用于变量:

ArrayList<Button> buttonsList = new ArraList<>();

add all your buttons to this list :

将所有按钮添加到此列表中:

buttonsList.add(button1);
....

do the same for your variables and put them in another arraylist of strings

对你的变量做同样的事情,并将它们放在另一个字符串数组列表中

ArrayList<String> labelsList = new ArrayList<>();

your for loop should look like this :

你的 for 循环应该是这样的:

for(int i = 0; i < labelList.size(); i++) {
    if(!labelList.get(i).equals("null")) {
        buttonsList.get(i).setVisibility(View.VISIBLE);
        buttonsList.get(i).setText(labelList.get(i));
    } else {
          buttonsList.get(i).setVisibility(View.INVISIBLE);
    }
}