Easier way to get view's Id (string) by its Id (int)

前端 未结 8 1340
暗喜
暗喜 2020-12-01 01:39

I\'m new with android and java, so sorry if it\'s a too basic question but I\'ve tried to find a solution in the forums and google and I couldn\'t.

I have 24 buttons

相关标签:
8条回答
  • 2020-12-01 02:34

    The approach is misguided to begin with. If you want to associate a piece of arbitrary data (e. g. a string) with a view, that's what tag is for. The ID is numeric and it better stay that way.

    EDIT much later: the OP's issue was a case of an XY problem. That said, the question title alone is a legitimate question in its own right.

    0 讨论(0)
  • 2020-12-01 02:38

    Edit:

    You have to use

    getResources().getResourceEntryName(int resid);

    If you want to retrieve the entry name associated to a resId

    or

    You can use getIdentifier() to retriece a resource identifier for the given resource name.

    For instance:

    int id = this.getResources().getIdentifier("yourtext", "string", this.getPackageName());
    
    0 讨论(0)
提交回复
热议问题