How to get the original resource identifier of a button?
In my layout xml file I have several buttons like this which share the same event handler:
<Button android:id="@+id/page1" onClick="OnClick" .... ></Button>
In my activity I would like something like this code:
public void onClick(View v) {
String url = String.format("http://example.com/%d", v.getId());
}
to dynamically build the url. However, getId() returns the integer representing the button ID, not the word "page1".
Of course I could use a switch statement, but it would be much more convenient if I could refer to the original ID name assi开发者_运维技巧gned to the button.
Is that possible?
Use getResources().getResourceName()
Reflection or, set the android:tag attribute, and then use .getTag(), simpler from code, but a bit redundancy in the.xml.
You can use reflections on generated R.id class. Or, better you can create own class with field containing value you want to know.
精彩评论