How to differentiate an EditText with the same name and id?

2
public void criaForm(){
    for (int i = 0; i < 5; i++) {
        EditText et = new EditText(this);
        linearLayout.addView(et);
        TextView tv = new TextView(this);
        linearLayout.addView(tv);
    }
}

I've created multiple EditTexts within a for , so they have the same name and id, is there any way I can differentiate them? Thank you in advance!

    
asked by anonymous 19.08.2015 / 15:38

2 answers

1

Any View Android has an attribute called tag which can be used to add extra information to a View .

An example of its use is the following:

View view = new View(contexto);
Object objeto = new Object();
view.setTag(objeto);

Object objectDaTag = view.getTag();

And since EditText is the child of View you can also take advantage of this feature. For example:

EditText et = new EditText(this);
et.setTag(objeto);
Object objetoDaTag = et.getTag(); // se atribuído outra classe deve ser feito o cast

Official Documentation

    
19.08.2015 / 19:40
0

If your problem is to differentiate two EditTexts and neither id nor tag satisfies you, take advantage of the inheritance and polymorphism mechanism of Java and create a class with an attribute over

public class MyEditText extends EditText {
    ...

}
    
21.08.2015 / 00:41