References to several Edittext

Asked

Viewed 42 times

1

It is possible to reference several elements at one time?

Ex:

An array with all tags and another with Edittext:

public class Main extends AppCompatActivity {

    String[] tags = {"edit1", "edit2", "edit3"};

    //Um array de edittext, creio não ser possível, mas é só para o exemplo
    EditText[] editText = {edit1, edit2, edit3};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //E então fizer
        for (int i = 0; tags.length < i; i++){
            editText[i].findViewWithTag( tags[i] );
        }
        //Assim fazendo referencia a todos editText com um for
    }
}

Is there any way to do that? Remembering, this example above the way I did is not possible, I know, is because I have more than 20 Edittexts in a layout, and I wanted a simpler way to reference all.

If there is a way with findViewById, it may be too, but I think that with findViewWithTag is easier

1 answer

2


If you want to build an Edittext array, you can do the following:

public class Main extends AppCompatActivity {

    String[] tags = {"edit1", "edit2", "edit3"};

    //Um array para guardar os EditText
    EditText[] editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Construir o array
        for (int i = 0; tags.length < i; i++){
            editText[i] = (EditText)findViewWithTag( tags[i] );
        }

    }
}

See other alternatives in this reply.

  • Can’t declare the names directly from the array no? More or less like I did in my example Edittext[] editText = {edit1, edit2, edit3};

  • Not because you don’t have the references. To use this way you would have to have an Edittext type variable for each of them and get their references, using findViewById(), before you can use them in editText = {edit1, edit2, edit3};

Browser other questions tagged

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