2011-03-03 33 views
0

如何在下面的代碼中訪問創建的EditTexts?我可以很容易地訪問從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

你需要將它們存儲在什麼地方。

頂你的類:

EditText[] columnEditTexts; 

在的onCreate:

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以及代碼保持不變。

這裏的代碼概率是column.setId(i);將id設置爲EditText,範圍從i = 1到i = MatrixMultiply.h1 -1。

並在聽者的onClick您通過使用 最終的EditText E1 =(的EditText)findViewById(R.id.entry1)找到SAM的EditText; 不確定entry1的值是多少。理想情況下,它應該介於i = 1到i = MatrixMultiply.h1 -1之間。

0

除了存儲數組或EditTexts的列表之外,還可以爲每個標籤附加一個唯一標籤。

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); 

。這隻會造成不必要的開銷。在循環之前一次完成。