2011-03-03 12 views
0

作成した編集テキストに次のコードでアクセスするにはどうすればよいですか?私は簡単にXMLで作成されたのEditTextボックスからテキストにアクセスすることができますが、どのように私は私のforループで見つかっ作成のEditTextボックスに入力されたものをキャプチャしない?:Android:作成したEditText内のテキストにアクセスするには?

public class Game extends Activity implements OnClickListener { 
    private static final String TAG = "Matrix"; 
    static int entry1; 
    static int entry2; 
    static int entry3; 
    static int entry4; 




@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    this.setContentView(R.layout.matrix); 
    View doneButton = findViewById(R.id.done_button); 
    doneButton.setOnClickListener(this); 

    for(int i = 0; i < MatrixMultiply.h1; i++){ 
     TableLayout table = (TableLayout)findViewById(R.id.myTableLayout); 
     TableRow row = new TableRow(this); 
     EditText column = new EditText(this); 
     for(int j = 0; j < MatrixMultiply.w1; j++){ 
      table = (TableLayout)findViewById(R.id.myTableLayout); 
      column = new EditText(this); 
      column.setId(i); 
      row.addView(column); 
     } 
     table.addView(row); 
    } 



} 

public void onClick(View v) { 
    switch (v.getId()) { 
    case R.id.done_button: 
     Intent k = new Intent(this, GameTwo.class); 

     final EditText e1 = (EditText) findViewById(R.id.entry1); 
     entry1 = Integer.parseInt(e1.getText().toString()); 

     final EditText e2 = (EditText) findViewById(R.id.entry2); 
     entry2 = Integer.parseInt(e2.getText().toString()); 

     final EditText e3 = (EditText) findViewById(R.id.entry3); 
     entry3 = Integer.parseInt(e3.getText().toString()); 

     final EditText e4 = (EditText) findViewById(R.id.entry4); 
     entry4 = Integer.parseInt(e4.getText().toString()); 

     startActivity(k); 
     //finish(); 
     break; 

    } 
} 
+2

あなたの質問がありますか? 'e1.getText()。toString()'を実行しているので、EditTextの中のテキストにアクセスしています。 – EboMike

+0

私はxmlで作成したEditTextボックスからテキストをキャプチャする方法を理解していますが、ループで作成されたEditTextボックスからテキストをキャプチャするにはどうすればよいですか? – Biggsy

+0

あなたは配列やどこかにそれらを格納する必要があります。 – EboMike

答えて

3

あなたがどこかにそれらを格納する必要があります。あなたのクラスの

トップ:のonCreateで

EditText[] columnEditTexts; 

columnEditTexts = new EditText[MatrixMultiply.w1]; 
for(int j = 0; j < MatrixMultiply.w1; j++){ 
     table = (TableLayout)findViewById(R.id.myTableLayout); 
     column = new EditText(this); 
     column.setId(i); 
     row.addView(column); 
     columnEditTexts[j] = column; 
    } 

そして、それを読んで...

for(int j = 0; j < MatrixMultiply.w1; j++){ 
    String value = columnEditTexts[j].getText().toString(); 

    // Do whatever you want to do with your value here 
} 
+0

どのようにそれらの値をキャプチャするのですか? – Biggsy

+0

あなたのコードが何をしようとしているのか分かりません。ループを繰り返すだけの場合は、この例を参照してください。 (編集開始) – EboMike

0

はコマンドがのEditTextからテキストをキャプチャすることに注意してくださいコードと同様にXMLからも同じです。

ここで、コード付きのprobは、のcolumn.setId(i)です。は、i = 1からi = MatrixMultiply.h1 -1の範囲にあるEditTextにidを設定します。

、あなたが最終 のEditText E1 =(のEditText)findViewById(R.id.entry1)を使用して、SAMのEditTextを見つけているのonClickリスナで、 entry1の値がわかりません。理想的には、i = 1〜i = MatrixMultiply.h1 -1の間にあるはずです。

0

EditTextの配列またはリストの保存以外に、それぞれに固有のタグを付けることもできます。

for(int i = 0; i < MatrixMultiply.h1; i++){ 
     TableLayout table = (TableLayout)findViewById(R.id.myTableLayout); 
     TableRow row = new TableRow(this); 
     EditText column = new EditText(this); 
     for(int j = 0; j < MatrixMultiply.w1; j++){ 
      table = (TableLayout)findViewById(R.id.myTableLayout); 
      column = new EditText(this); 
      column.setId(i); 
      column.setTag(new Point(i, j)); 
      row.addView(column); 
     } 
     table.addView(row); 
    } 

その後、後:

Point currentRowColumn = new Point(0, 0); 
int currentRow = 0; 
int currentColumn = 0; 
EditText currentEditText = findViewWithTag(currentRowColumn); 
while (currentEditText != null) { 
    while (currentEditText != null) { 
     String text = currentEditText.getText().toString(); 
     // Do stuff with the text here. 
     currentRowColumn.y += 1; 
     currentEditText = findViewWithTag(currentRowColumn); 
    } 
    // If we reach here, then findViewWithTag returned null, 
    // meaning we've finished the row. Move on to the next row. 
    currentRowColumn.x += 1; 
    currentRowColumn.y = 0; 
    currentEditText = findViewWithTag(currentRowColumn); 

} 

そこにいくつかの醜い繰り返しがあります、私は、行対混乱して列を有していてもよく、及び(タグは任意のオブジェクトであってもよい)の例では、おそらくandroid.graphics.Pointを悪用、それ基本的にアイデアを示しています。列と行の数を保存した場合、ネストされたwhileループは、よりきれいなネストされたforループに変換できます。

0

プラス、あなたはforループで

TableLayout table = (TableLayout)findViewById(R.id.myTableLayout); 

ようSTHを行うべきではありません。不要なオーバーヘッドが発生するだけです。ループの直前にそれを一回行います。

関連する問題