如何获取 ImageView 的来源以便更改它? [英] how to get the source of ImageView in order to change it?

查看:22
本文介绍了如何获取 ImageView 的来源以便更改它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道仅使用 myImageView.setImageResource(mynewImageDrawable)

但我想做的是在更改之前检查当前的 ImageSource.

but what I want to do is to check the current ImageSource before changing it.

基本上,我想使用 imageViews 实现我自己的组单选按钮.所以每次点击任何imageView时,onclicked事件方法都会改变我组的Image Resources.

basically, I want to implement my own group radio buttons using imageViews. so every time any imageView is clicked, the oncliked event method will change the Image Resources of my group.

对不起,我的英语很差.

and sorry for my poor English.

问候,红海

推荐答案

没有 getDrawableId 函数,所以你需要为 ImageView 当您更改其可绘制对象时.例如,将 drawable id 设置为 ImageView 的标签,这样您就可以从标签中获取 drawable id.

There is no getDrawableId function so you'll need to do something like set a tag for the ImageView when you change its drawable. For instance, set the drawable id as a tag for the ImageView so you could just get the drawable id from the tag.

我会说 90% 的时间,你的视图上不会有任何标签,所以最简单的方法是假设你的标签是唯一的标签:

I'd say 90% of the time, your views wont have any tag on them, so the easiest way is to assume your tag is the only tag:

myImageView.setTag(R.drawable.currentImage);    //When you change the drawable
int drawableId = (Integer)myImageView.getTag(); //When you fetch the drawable id

如果我的视图中已经有标签怎么办

Android 视图可以同时托管多个标签,只要它们具有唯一标识符即可.您需要 创建一个唯一的 id 资源和将它作为第一个参数添加到 setTag 方法调用.留下这样的代码:

What if I already have a tag on my view

Android views can host multiple tags at the same time, as long as they have a unique identifier. You'd need to create a unique id resource and add it as the first argument to the setTag method call. Leaving the code like this:

myImageView.setTag(R.id.myTagId, R.drawable.currentImage); //Set
int drawableId = (Integer)myImageView.getTag(R.id.myTagId);

这篇关于如何获取 ImageView 的来源以便更改它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆