【问题标题】:Android: Get the image source name of an ImageView to display on a TextViewAndroid:获取要在 TextView 上显示的 ImageView 的图像源名称
【发布时间】:2011-06-24 04:11:38
【问题描述】:

您好,我是 android 开发的新手,但在这方面遇到了麻烦。当我单击某个图像时,我想在 textView 上显示我的 imageView 的 src 名称。 要进一步了解这里是我对image clickListener 的硬代码(希望有人也可以帮助我改进我的编码)。

    int ctr = 0;
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.play);

        //DECLARE CLICK LISTENERS ON OBJECTS
        ImageView ImageView1 = (ImageView)findViewById(R.id.imageView1);
        ImageView1.setOnClickListener(this);
        ImageView ImageView2 = (ImageView)findViewById(R.id.imageView2);
        ImageView2.setOnClickListener(this);
        ...

public void onClick(View v) {
        //CHANGE TEXT VIEW TEXT BASED ON THE CTR VALUE
        TextView TextView1 =(TextView)findViewById(R.id.textView1); 

        //I WANT TO CHANGE THIS ONE TO DISPLAY SRC INSTEAD OF THE CTR VALUE
        TextView1.setText(Integer.toString(ctr)); 
}

在那里,我不想显示 ctr 值,而是显示我在设置的两个图像之间单击的任何图像的 src 名称。我相信这是可能的。谢谢。

【问题讨论】:

  • 您正在显示来自可绘制文件夹或任何其他资源的 img?

标签: android


【解决方案1】:

在xml中你可以添加标签来设置任何带有图像的信息

示例

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

您可以动态地使用imageView.setTag("any name") 设置任何数据以及图像,并使用imageView.getTag() 检索图像信息

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

【讨论】:

  • 谢谢,帮了大忙。
【解决方案2】:

您可以使用以下方法从它的 id 中获取资源名称:

/**
         * Determines the Name of a Resource,
         * by passing the <code>R.xyz.class</code> and
         * the <code>resourceID</code> of the class to it.
         * @param aClass : like <code>R.drawable.class</code>
         * @param resourceID : like <code>R.drawable.icon</code>
         * @throws IllegalArgumentException if field is not found.
         * @throws NullPointerException if <code>aClass</code>-Parameter is null.                  
         * <code>String resName = getResourceNameFromClassByID(R.drawable.class, R.drawable.icon);</code><br>
         * Then <code>resName</code> would be '<b>icon</b>'.*/
        public String getResourceNameFromClassByID(Class<?> aClass, int resourceID)
                                                throws IllegalArgumentException{
                /* Get all Fields from the class passed. */
                Field[] drawableFields = aClass.getFields();

                /* Loop through all Fields. */
                for(Field f : drawableFields){
                        try {
                                /* All fields within the subclasses of R
                                 * are Integers, so we need no type-check here. */

                                /* Compare to the resourceID we are searching. */
                                if (resourceID == f.getInt(null))
                                        return f.getName(); // Return the name.
                        } catch (Exception e) {
                                e.printStackTrace();
                        }
                }
                /* Throw Exception if nothing was found*/
                throw new IllegalArgumentException();
        }

使用示例:

String resName = getResourceNameFromClassByID(R.drawable.class, R.drawable.icon);

结果将是icon

【讨论】:

  • @如果你得到你的解决方案而不是投票并点击这个答案
【解决方案3】:

试试这个

  String resName = getResourceNameFromClassByID(R.drawable.class, R.drawable.imagename);

【讨论】:

  • 让你测试这个代码请上传代码如何做到这一点
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-07-07
  • 2014-05-08
  • 2020-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多