Android:如何访问创建的 EditText 中的文本?
如何访问以下代码中创建的 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;
}
}
How do I access the created EditTexts in the following code? I can easily access the text from the EditText boxes created in the xml, but how do I capture what is entered into the created EditText boxes found in my for loop?:
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你需要将它们存储在某个地方。
班上的佼佼者:
在 onCreate 中:
并阅读它......
You need to store them somewhere.
Top of your class:
In onCreate:
And reading it...
请注意,从 EditText 捕获文本的命令与 xml 以及代码中的命令保持相同。
这里代码的问题是 column.setId(i); 将 id 设置为 EditText,范围从 i=1 到 i = MatrixMultiply.h1 -1。
在 onClick 侦听器中,您可以使用以下方法找到相同的 EditText
final EditText e1 = (EditText) findViewById(R.id.entry1);
不确定 Entry1 的值是什么。理想情况下,它应该介于 i=1 到 i = MatrixMultiply.h1 -1 之间。
Please note that the command to capture the text from EditText remains same from xml as well as by the code.
Here the prob with the code is the column.setId(i); sets id to EditText ranging from i=1 to i = MatrixMultiply.h1 -1.
and in the onClick listener you are finding the sam EditText by using
final EditText e1 = (EditText) findViewById(R.id.entry1);
Not sure whats the value of entry1 . Ideally it should be something between i=1 to i = MatrixMultiply.h1 -1.
除了存储 EditText 的数组或列表之外,您还可以为每个 EditText 附加一个唯一的标签。
后来:
那里有一些丑陋的重复,我可能混淆了列与行,并且该示例可能误用了
android.graphics.Point
(尽管标签可以是任何对象),但它基本上显示了主意。如果您保存了列数和行数,则嵌套的 while 循环可以转换为更漂亮的嵌套 for 循环。Aside from storing an array or List of the EditTexts, you could also attach a unique tag to each one.
Then later:
There's some ugly repetition there, I may have confused columns vs. rows, and the example perhaps misuses
android.graphics.Point
(although a tag can be any object), but it basically shows the idea. If you saved the number of columns and rows, the nested while loops could be converted into much prettier nested for loops.那样做某事
另外,你不应该像在 for 循环中 。它只会产生不必要的开销。在循环之前执行一次。
Plus, you shouldn't do sth like
in a for-loop. It just creates unnecessary overhead. Do it ONCE right before the loop.