开发者

Android: How to access text inside created EditText?

开发者 https://www.devze.com 2023-02-14 06:44 出处:网络
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 boxe

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);
开发者_C百科   }



}

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;

    }
}


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
}


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号