gpt4 book ai didi

java - Android:如何访问创建的 EditText 中的文本?

转载 作者:行者123 更新时间:2023-12-02 00:45:12 25 4
gpt4 key购买 nike

如何访问以下代码中创建的 EditText?我可以轻松地从 xml 中创建的 EditText 框中访问文本,但是如何捕获在 for 循环中找到的创建的 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;

}
}

最佳答案

您需要将它们存储在某个地方。

类(class)名列前茅:

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
}

关于java - Android:如何访问创建的 EditText 中的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5175149/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com