简体   繁体   中英

How can I get the associated Id of a View as a string?

When you create a layout, you define an id for your views like this:

<ImageView
   android:id="@+id/myImage"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:src="@drawable/main01" />

and in your code you have:

ImageView img = (ImageView) findViewById(R.id.myImage);

now you have the object of img .

How can I get the id of img ("myImage") as a string ? I need something like this:

String strId = getStringId(img);
// now strId is "myImage"

Try this.

Log.i("============ Id","::"+getResources().getResourceEntryName(img.getId()));

Output : ============ Id :: myImage

这是你在找什么?

String value = getResources().getString(R.id.myImage);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM