安卓:ImageView的的getID();返回整数整数、ImageView、getID

2023-09-05 00:49:21 作者:网混子

我已经成立了一个onTouch类,以确定当我的40个按键一​​个是pressed。

I've set up an onTouch class to determine when one of my 40 buttons is pressed.

我现在面临的问题是确定的这的按钮是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

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

 }

+ - + - + - + - + - + -

+-+-+-+-+-+-

我其他明智的优良工程,它知道什么按钮,你是pressing。我很新的这个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.

推荐答案

您不能拿到 widgetA1 串......你总是会得到一个整数。但是,整数独有的控制。

You cannot get that widgetA1 string...You will always get an integer. But that integer is unique to that control.

所以你可以做这个检查,其中按钮pressed

so you can do this to check, which button is pressed

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

}