Android:获取要在TextView上显示的ImageView的图像源名称

时间:2011-06-24 04:11:38

标签: android

嗨,我是Android开发的新手,我遇到了这个问题。当我点击某个图片时,我想在imageView上显示textView的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名称。我相信这是可能的。感谢。

3 个答案:

答案 0 :(得分:20)

在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();

答案 1 :(得分: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

答案 2 :(得分:-1)

试试这个

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