Android : ImageView getID(); returning integer(Android : ImageView getID();返回整数)
问题描述
我设置了一个 onTouch 类来确定我的 40 个按钮中的一个何时被按下.
I've set up an onTouch class to determine when one of my 40 buttons is pressed.
我面临的问题是确定哪个按钮被按下了.
The problem I am facing is determining which button was pressed.
如果我使用:
int ID = iv.getId();
int ID = iv.getId();
当我点击按钮widgetA1"时
When I click on button "widgetA1"
我收到以下 ID:
2131099684
我希望它返回字符串 IDwidgetA1"
I would like it to return the string ID "widgetA1"
来自:game.xml
from:game.xml
<ImageView android:layout_margin="1dip" android:id="@+id/widgetA1" android:src="@drawable/image" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
来自:game.java
from:game.java
public boolean onTouch(View v, MotionEvent event) {
ImageView iv = (ImageView)v;
int ID = iv.getId();
String strID = new Integer(ID).toString();
Log.d(TAG,strID);
//.... etc
}
+-+-+-+-+-+-
+-+-+-+-+-+-
否则我工作得很好,它知道你按的是什么按钮.我对这个 Android JAVA 很陌生.让我知道你们是否可以帮助我.
I other wise works fine, it knows what button you are pressing. I am quite new to this Android JAVA. Let me know if you guys can help me.
推荐答案
Edit - TL;DR:
Edit - TL;DR:
View v; // handle to your view
String idString = v.getResources().getResourceEntryName(v.getId()); // widgetA1
原文:
我知道你发帖已经有一段时间了,但我正在处理一个类似的问题,我想我通过查看 Android View 类的源代码.
I know it's been a while since you posted, but I was dealing with a similar problem and I think I found a solution by looking at the Android source code for the View class.
我注意到当您打印视图时(隐式调用 toString()),打印的数据包括布局文件中使用的 ID 字符串(您想要的),而不是 getId() 返回的整数.所以我查看了 View 的 toString() 的源代码,以了解 Android 是如何获取这些信息的,它实际上并不太复杂.试试这个:
I noticed that when you print a View (implicitly calling toString()), the data printed includes the ID String used in layout files (the one you want) instead of the integer returned by getId(). So I looked at the source code for View's toString() to see how Android was getting that info, and it's actually not too complicated. Try this:
View v; // handle to your view
// -- get your View --
int id = v.getId(); // get integer id of view
String idString = "no id";
if(id != View.NO_ID) { // make sure id is valid
Resources res = v.getResources(); // get resources
if(res != null)
idString = res.getResourceEntryName(id); // get id string entry
}
// do whatever you want with the string. it will
// still be "no id" if something went wrong
Log.d("ID", idString);
在 源代码,Android也使用getResourcePackageName(id)
和getResourceTypeName(id)
来构建完整的字符串:
In the source code, Android also uses getResourcePackageName(id)
and getResourceTypeName(id)
to build the full string:
String idString = res.getResourcePackageName(id) + ":" + res.getResourceTypeName(id)
+ "/" + res.getResourceEntryName(id);
这会导致 android:id/widgetA1
的效果.
希望对您有所帮助!
This results in something to the effect of android:id/widgetA1
.
Hope that helps!
这篇关于Android : ImageView getID();返回整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Android : ImageView getID();返回整数
基础教程推荐
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01