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

发布于 2024-10-19 18:35:46 字数 1758 浏览 5 评论 0原文

如何访问以下代码中创建的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

策马西风 2024-10-26 18:35:46

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

班上的佼佼者:

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
}

You need to store them somewhere.

Top of your class:

EditText[] columnEditTexts;

In 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;
   }

And reading it...

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

    // Do whatever you want to do with your value here
}
‘画卷フ 2024-10-26 18:35:46

请注意,从 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.

深海蓝天 2024-10-26 18:35:46

除了存储 EditText 的数组或列表之外,您还可以为每个 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 循环。

Aside from storing an array or List of the EditTexts, you could also attach a unique tag to each one.

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

Then later:

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

}

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.

━╋う一瞬間旳綻放 2024-10-26 18:35:46

那样做某事

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

另外,你不应该像在 for 循环中 。它只会产生不必要的开销。在循环之前执行一次。

Plus, you shouldn't do sth like

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

in a for-loop. It just creates unnecessary overhead. Do it ONCE right before the loop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文