How to find View from string instead of R.id

前提是你 提交于 2019-12-20 17:26:22

问题


Let's assume I have this in layout of res in my app

    <TextView android:id="@+id/titleText" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:text="@string/app_name"
        android:textColor="#ffffffb0" android:padding="5px" />

In my activity, I get the TextView using this command

 TextView tv = (TextView)findViewById(R.id.titleText);

But I am looking for another method like this

 TextView tv = (TextView)findViewByString("R.id."+"titleText");

because I need to enumerate those ids. Can any of you give a hint or clue how I can go about it? Thanks


回答1:


You can use something like this:

Resources res = getResources();
int id = res.getIdentifier("titleText", "id", getContext().getPackageName());

And then use the id.



来源:https://stackoverflow.com/questions/8438943/how-to-find-view-from-string-instead-of-r-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!