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

前端 未结 8 1334
暗喜
暗喜 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条回答
  •  -上瘾入骨i
    2020-12-01 02:33

    like this:

    /**
     * @return "[package]:id/[xml-id]"
     * where [package] is your package and [xml-id] is id of view
     * or "no-id" if there is no id
     */
    public static String getId(View view) {
        if (view.getId() == View.NO_ID) return "no-id";
        else return view.getResources().getResourceName(view.getId());
    }
    

    I use this in view constructors to make more meaningful TAGs

提交回复
热议问题