2

私は3つのテキストボックス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 の 3 つのテキスト ボックスがあります。

まずあなたの問題があります。3 つの個別の変数を使用する代わりに、配列またはコレクションを作成します。

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 に答える