how to get the source of ImageView in order to change it?(如何获取 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 的来源以便更改它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何获取 ImageView 的来源以便更改它?
基础教程推荐
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
