2

我有三个文本框 ct1、ct2、ct3。我必须使用 for 循环 1 到 3 并检查文本框是否为空。那么,在 for 循环中,我该如何表示呢?例如,

for(i=0;i<=3;i++)
{
    if(ct+i.getText()) // I know I'm wrong
     {
     }

}
4

3 回答 3

7

我有三个文本框 ct1、ct2、ct3。

有你的问题开始。不要使用三个单独的变量,而是创建一个数组或集合:

TextBox[] textBoxes = new TextBox[3];
// Populate the array...

或者:

List<TextBox> textBoxes = new ArrayList<TextBox>();
// Populate the list...

然后在你的循环中:

// Note the < here - not <=
for (int i = 0; i < 3; i++) {
   // If you're using the array
   String text = textBoxes[i].getText();

   // or for the list...
   String text = textBoxes.get(i).getText();
}

或者,如果您不需要索引:

for (TextBox textBox : textBoxes) {
    String text = textBox.getText();
    ...
}
于 2012-08-02T10:42:00.190 回答
2

使用数组

TextBox[] boxes = new TextBox[]{ct1,ct2,ct3};
for(i=0;i<3;i++)
{
    boxes[i].getText(""); // I know I'm wrong
}
于 2012-08-02T10:43:47.407 回答
1

您可以将文本框放在列表中并遍历该列表:

List<TextBox> ctList = new ArrayList<TextBox> ();
list.add(ct1);
list.add(ct2);
list.add(ct3);

for (TextBox ct : ctList) {
    if(ct.getText().equals("expected text")) {
        // do your stuff here
    }
}
于 2012-08-02T10:50:46.867 回答