如何在下面的代碼中訪問創建的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;
}
}
你的問題是什麼?您正在執行'e1.getText()。toString()',因此您正在訪問EditText中的文本。 – EboMike
我明白如何捕獲我在xml中創建的EditText框中的文本,但是如何捕獲在我的循環中創建的EditText框中的文本? – Biggsy
您需要將它們存儲在數組或某處。 – EboMike