You could do this via reflection as Quoi points out, but this is generally not a good a idea.
It would be better just to add your objects to a list or a map:
List<EditText> list = new ArrayList<EditText>();
EditText editTxt1 = (EditText) findViewById(R.id.editText1);
list.add(editTxt1);
EditText editTxt2 = (EditText) findViewById(R.id.editText2);
list.add(editTxt2);
EditText editTxt3 = (EditText) findViewById(R.id.editText3);
list.add(editTxt1);
EditText editTxt4 = (EditText) findViewById(R.id.editText4);
Now we can cycle through the list and use the index to check which edit text was being called.
int i = 0;
for (EditText e : list) {
if(e.getText().toString().equals("something")) {
System.out.println("editText" + i + " equals something");
// do stuff
}
i++;
}
You could also use a Map to do this, this would allow you to have a name value against your objects to help give you a better reference to check what object was being called. This takes a bit more work, but might be use
Map<EditText, String> map = new HashMap<EditText, String>();
EditText editTxt1 = (EditText) findViewById(R.id.editText1);
map.put(editTxt1, "editTxt1");
EditText editTxt2 = (EditText) findViewById(R.id.editText2);
list.add(editTxt2, "editTxt2");
for (Entry<String, String> e : map.entrySet()) {
if(e.getValue().equals("something")) {
System.out.println(e.getKey() + " was equal to somethihng");
}
}
PS - Never use ==
to compare Strings, it won't always work!!