Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:ImageView getID();返回整数_Android_Button_Imageview_Ontouchlistener - Fatal编程技术网

Android:ImageView getID();返回整数

Android:ImageView getID();返回整数,android,button,imageview,ontouchlistener,Android,Button,Imageview,Ontouchlistener,我已经设置了一个onTouch类来确定何时按下我的40个按钮中的一个 我面临的问题是确定按下了哪个按钮 如果我使用: int ID=iv.getId() 当我点击按钮“widgetA1” 我收到以下ID: 21301099684 我希望它返回字符串ID“widgetA1” 来源:game.xml <ImageView android:layout_margin="1dip" android:id="@+id/widgetA1" android:src="@drawable/image" a

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

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

如果我使用:

int ID=iv.getId()

当我点击按钮“widgetA1”

我收到以下ID:

21301099684

我希望它返回字符串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>
+-+-+-+-+-+-


我知道其他的方法很好,它知道你在按什么按钮。我对这个Android JAVA非常陌生。如果你们能帮我,请告诉我。

你们不能得到那个
widgetA1
字符串……你们总是会得到一个整数。但该整数是该控件所特有的

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

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

}

我想你可以用这个 试试看


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

android:hint="buttonName"
然后在onClicked函数(.java文件)中,编写以下内容以获得提示:

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

Src


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

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

这是示例用法。

编辑-TL;博士:

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

我知道你发帖子已经有一段时间了,但我遇到了一个类似的问题,我想我通过查看Android找到了一个解决方案

我注意到,当您打印视图(隐式调用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);
在中,Android还使用
getResourcePackageName(id)
getResourceTypeName(id)
构建完整字符串:

String idString = res.getResourcePackageName(id) + ":" + res.getResourceTypeName(id)
    + "/" + res.getResourceEntryName(id);
这会产生类似于android:id/widgetA1的效果。


希望有帮助

当然,您可以获得
widgetA1
,只需执行以下操作:

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

它应该为您提供
您的.package.name:id/widgetA1
,然后对该字符串执行您想要的操作。

实际上,您可以查看Bodecker Answer,您得到的是R.id中定义的id。看看这篇文章,也许会有帮助
View v; // handle to your view
String idString = v.getResources().getResourceEntryName(v.getId()); // widgetA1
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);
String idString = res.getResourcePackageName(id) + ":" + res.getResourceTypeName(id)
    + "/" + res.getResourceEntryName(id);
ImageView iv = (ImageView)v;
int id = iv.getId();
String idStr = getResources().getResourceName(id);