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

Asked

Viewed 97 times

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 created several EditTexts within a for, with this they have the same name and id, is there any way where I can differentiate them? I thank you from now!

  • Dude... it’s not clear your question... try to express yourself better and put some code pieces so we can help you!

  • Hello, I just wanted to differentiate one Edittext from another by its name or id. I put a piece of my code where I create it. In my code I have 5 et. I wanted for example, when clicking on the second et, to know that I clicked on it.

2 answers

1

Whichever View Android has an attribute called tag which can be used to add extra information in 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 son of View can also take advantage of this resource. As 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

  • Thanks for the answers, but I’ve tried with tag and had the same id problem, Edittext "picks" only the value of the last tag.

  • And if generating unique Ids for these Edittext solves your problem?

  • I asked this because I believe you are not assigning enhanced Ids to prevent conflict with R-class Ids.

  • I generated, I used the counter from the is concatenated with another number "(i+5)", the id is unique that I tested this by placing everyone inside an Arraylist, the problem I’m having is to use the textWatcher, i tried putting all editTexts inside an Edittext Arraylist but it only works if I set the editText value in hand "listEditText.get(position)" ,I would need to click editText and it returns me some value that could differentiate it from others

  • I’m creating everything dynamically, I’m not using xml for anything.

  • I know no other way to identify a View in a unique and efficient way than with id.

Show 1 more comment

0

If your problem is to differentiate two Edittexts and neither id nor tag satisfy you, take advantage of the Java heritage and polymorphism mechanism and create a class with an extra attribute

public class MyEditText extends EditText {
    ...

}

Browser other questions tagged

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