私は3つのテキストボックスct1、ct2、ct3を持っています。forループ1から3を使用して、テキストボックスが空かどうかを確認する必要があります。では、forループ内で、どのように表現すればよいのでしょうか。例えば、
for(i=0;i<=3;i++)
{
if(ct+i.getText()) // I know I'm wrong
{
}
}
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();
...
}
配列を使用する
TextBox[] boxes = new TextBox[]{ct1,ct2,ct3};
for(i=0;i<3;i++)
{
boxes[i].getText(""); // I know I'm wrong
}
テキスト ボックスをリストに入れ、そのリストを反復処理できます。
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
}
}