How to get the image name of the ImageView in Android?

a 夏天 提交于 2019-12-12 08:01:21

问题


I have an imageview in android . I want to get the name of the image which is set in the in the imageview. How can I do that ?

ImageView v = (ImageView)findViewbyId(R.id.img);
//String name = findImageName(v);
if( name.equals(a))
{
   //do something
} 
else{
   //do something
}

回答1:


For this first of all you need to set a tag for your ImageView

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageview1" 
    android:background="@drawable/bg1"
    android:tag="bg1"/>

Now get the image name currently set on your ImageView by doing the following,

ImageView v = (ImageView)findViewbyId(R.id.img);
String backgroundImageName = String.valueOf(v.getTag());

and this should return the image tag name which is "bg1".

Now compare the tag names or use it for further process

if( backgroundImageName.equals(bg1))
{
   //do something
} 
else{
   //do something
}



回答2:


You can set the name ussing tag.

imageView.setTag("any name")

And retrieve the value using

String imageNameName = (String) imageView.getTag();



回答3:


    ImageView img = (ImageView) findViewById(R.id.imageView);
    String imageName = img.getContentDescription();



回答4:


Couldn´t you use something like this?

ImageView v = (ImageView)findViewbyId(R.id.img);
if(R.id.someId == v.getId())
{
   //do something
} 
else{
   //do something
}

It´s easier than use resources' names.



来源:https://stackoverflow.com/questions/31651209/how-to-get-the-image-name-of-the-imageview-in-android

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