How to rescue Edittext values created via script?

Asked

Viewed 33 times

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 redeem the values of each field?

  • If I understand the question it will be so pairs[0].getText().toString()

  • OK @ramaral, but this from the code I posted? the new EditText() serves to redeem values too? vc can be more specific?

  • Based on what you posted I can’t be more specific. I’m not even sure if that’s what you want.

  • Now even more confused I got "the new Edittext() serves to redeem values too?" What do you mean by "redeem"?

  • @ramaral code I posted creates 4 Edittext type fields. When I say I want to redeem values, I’m saying I want to take what was typed in each field.

1 answer

2


To be able to access the content typed in each Edittext use:

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

where indice is the position of Edittext in the array whose content it seeks to obtain.

If you want access to array pairs anywhere in your Activity shall declare it as a field of.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.