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 - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01