Android : ImageView getID();返回整数

Android : ImageView getID(); returning integer(Android : ImageView getID();返回整数)

本文介绍了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();返回整数

基础教程推荐