如何在Android中获取ImageView的图像名称?

时间:2015-07-27 10:53:34

标签: android image imageview

我在android中有一个imageview。我想获取在imageview中设置的图像的名称。我怎样才能做到这一点 ?

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

4 个答案:

答案 0 :(得分:7)

首先,您需要为ImageView设置标签

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

现在通过执行以下操作获取当前在ImageView上设置的图像名称

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

这应返回“bg1”的图片标记名称。

现在比较标签名称或将其用于进一步处理

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

答案 1 :(得分:3)

您可以设置名称ussing tag。

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

使用

检索值
        now := time.Now()
        location, _ := time.LoadLocation("Atlantic/Cape_Verde")
        timeAtZone := now.In(location)
        fmt.Println(timeAtZone)
        timestamp = timeAtZone.Unix()
        fmt.Println(timestamp)
        fmt.Println(now.Add(-time.Hour).UTC().Unix())
        fmt.Println(now.UTC().Unix())

答案 2 :(得分:1)

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

答案 3 :(得分:1)

难道你不能使用这样的东西吗?

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

比使用资源名称更容易。