References to various EditText

1

Is it possible to reference multiple elements at once?

Ex:

An array with all tags and another array 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 this? Remembering, this example above the way I did it is not possible, I know, it's because I have over 20 EditTexts in one layout, and I wanted a simpler way to reference them all.

If there is a mode with findViewById, it might be too, but I think findViewWithTag is easier

    
asked by anonymous 31.03.2018 / 19:23

1 answer

2

If you want to build an array of EditText, 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 response .

    
01.04.2018 / 00:04