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

KaH*_*HeL 2 android

嗨,我是Android开发的新手,我遇到了这个问题.我要显示我的src名称imageViewtextView,当我点击一个特定的形象.为了进一步了解这里是我的硬编码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)); 
}
Run Code Online (Sandbox Code Playgroud)

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

Sun*_*hoo 21

在xml中,您可以添加标签以使用图像设置任何信息

<ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageview1" 
        android:background="@drawable/bg"
        android:tag="bg"/>
Run Code Online (Sandbox Code Playgroud)

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

String backgroundImageName = (String) imageView.getTag();
Run Code Online (Sandbox Code Playgroud)