How to retrieve values from EditText created via script?

3

I have the following code that creates 4 EditText in Layout:

LinearLayout myLayout = (LinearLayout) findViewById(R.id.formulario);
    ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT,    ViewGroup.LayoutParams.WRAP_CONTENT);
    EditText[] pairs=new EditText[4];
    for(int l=0; l<4; l++)
    {
        pairs[l] = new EditText(this);
        pairs[l].setTextSize(15);
        pairs[l].setLayoutParams(lp);
        pairs[l].setId(l);
        pairs[l].setText((l + 1) + ": something");
        myLayout.addView(pairs[l]);
    }

How do I retrieve values from each field?

    
asked by anonymous 11.01.2016 / 04:24

1 answer

2

In order to access the content you type in each of the EditText use:

String textoDigitado = pairs[indice].getText().toString();

where is the position of the EditText in array whose content you want to get.

If you want to have access to array indice anywhere in your Activity you should declare it as an attribute field.

private EditText[] pairs;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ....
    ....
    LinearLayout myLayout = (LinearLayout) findViewById(R.id.formulario);
    ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT,    ViewGroup.LayoutParams.WRAP_CONTENT);
    pairs = new EditText[4];
    for(int l=0; l<4; l++)
    {
        pairs[l] = new EditText(this);
        pairs[l].setTextSize(15);
        pairs[l].setLayoutParams(lp);
        pairs[l].setId(l);
        pairs[l].setText((l + 1) + ": something");
        myLayout.addView(pairs[l]);
    }
}
    
11.01.2016 / 15:24