Android : ImageView getID(); returning integer

前提是你 提交于 2020-01-11 15:47:06

问题


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.

If I use:

int ID = iv.getId();

When I click on button "widgetA1"

I receive the following ID:

2131099684

I would like it to return the string ID "widgetA1"

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>

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

 }

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

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.


回答1:


Edit - TL;DR:

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

Original:

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.

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

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

This results in something to the effect of android:id/widgetA1.

Hope that helps!




回答2:


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

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.
{

}



回答3:


Xml

android:tag="buttonA"

Src

Button.getTag().toString();



回答4:


Of course you can get widgetA1, simply do:

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

It should give you your.package.name:id/widgetA1, then do what you want with that string.




回答5:


We will get the Integer value corresponds to the views id. For finding on which button you are clicking, better to use tags. you can set tag in the layout xml file and can compare which item you have clicked using.

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

This is sample usage.




回答6:


i think that you can use this try it

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



回答7:


For the implementation you could use the hint attribute to store the button name, which is accessible. For example in the button code (.xml file) use the following line:

android:hint="buttonName"

Then in the onClicked function (.java file), write the following to get the hint:

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


来源:https://stackoverflow.com/questions/3920833/android-imageview-getid-returning-integer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!