Android:ImageView getID();返回整数

时间:2010-10-13 04:44:43

标签: android button imageview ontouchlistener

我已经设置了一个onTouch类来确定何时按下了我的40个按钮之一。

我面临的问题是确定按了哪个按钮。

如果我使用:

int ID = iv.getId();

当我点击“widgetA1”按钮

我收到以下ID:

2131099684

我希望它返回字符串ID“widgetA1”

这: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

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很新。如果你们能帮助我,请告诉我。

7 个答案:

答案 0 :(得分:18)

你不能得到那个widgetA1字符串...你总会得到一个整数。但是该整数对于该控件是唯一的。

所以你可以这样做来检查,按下哪个按钮

int ID = iv.getId();
if(ID == R.id.widgetA1) // your R file will have all your id's in the literal form.
{

}

答案 1 :(得分:18)

编辑 - TL; DR:

View v; // handle to your view
String idString = v.getResources().getResourceEntryName(v.getId()); // widgetA1

原件:

我知道自从你发布以来已经有一段时间了,但我正在处理类似的问题,我认为我通过查看Android source code for the View class找到了解决方案。

我注意到当你打印一个View(隐式调用toString())时,打印的数据包括布局文件中使用的ID字符串(你想要的那个),而不是getId()返回的整数。所以我查看了View的toString()的源代码,看看Android是如何获取该信息的,实际上并不是太复杂。试试这个:

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);

source code中,Android还使用getResourcePackageName(id)getResourceTypeName(id)来构建完整的字符串:

String idString = res.getResourcePackageName(id) + ":" + res.getResourceTypeName(id)
    + "/" + res.getResourceEntryName(id);

这导致了android:id/widgetA1的影响。

希望有所帮助!

答案 2 :(得分:15)

XML

android:tag="buttonA"

的Src

Button.getTag().toString();

答案 3 :(得分:3)

当然,你可以获得widgetA1,只需这样做:

ImageView iv = (ImageView)v;
int id = iv.getId();
String idStr = getResources().getResourceName(id);

它应该给你your.package.name:id/widgetA1,然后用那个字符串做你想做的事。

答案 4 :(得分:2)

我们将得到Integer值对应的视图id。要查找您要点击的按钮,最好使用标签。您可以在布局xml文件中设置标记,并可以比较您单击使用的项目。

<Button 
        android:id="@+id/btnTest"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Button"
        android:tag="btntestTag"/>

这是示例用法。

答案 5 :(得分:0)

我认为你可以使用它 试试吧

 ImageView imgView = new ImageView(this);
String imgName = "img"//in your case widgetA1
int id = getResources().getIdentifier(imgName, "drawable", getPackageName());

答案 6 :(得分:0)

对于实现,您可以使用hint属性来存储可访问的按钮名称。例如,在按钮代码(.xml文件)中,使用以下行:

android:hint="buttonName"

然后在onClicked函数(.java文件)中,编写以下内容以获取提示:

String hint = ((Button)v).getHint().toString(); //v is the view passed onClick