Android:点击时获取动态生成的ImageButton的id
我为每个 ImageButton 动态生成了具有不同 ImageResource 的 ImageButtons。现在我想知道单击了哪个 ImageButton,如何确定? 需要你的帮助。 谢谢。
I have dynamically generated ImageButtons with different ImageResource for each ImageButton. Now I want to know which ImageButton was clicked, how can I determine this ?
Need your help.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以为每个创建的
ImageButton
设置一个id,并为检查单击的女巫按钮设置getId()
you can set an id for each created
ImageButton
andgetId()
for check witch button clicked为了做到这一点,你可以做两件事:
首先,当动态生成 ImageButton 时,你可以调用 setId() 来为此 View 设置特定的 id 并将其存储在 List 等中。
然后,当你有一个单击事件时(或者其他),你可以调用View的getId()方法来获取id。
然后你就可以比较并做任何你想做的事情。
希望这有帮助!
In order to do this you could do two things:
Firstly, when dynamically generated the ImageButton you could call setId() in order to set a specific id to this View and store it in List, etc.
Then when you have a click event (or anything else), you can call the getId() method of the View to get the id.
Then you can compare and do anything you want.
Hope this helps!
任何资源都由 R.java 文件中生成的 id 唯一标识。
所以你可以使用类似的东西:
Any resource is uniquely identified by its id which is generated in R.java file.
So you can use something like :
如果您的代码生成 imageButtons 那么,在这段代码中您可以编写类似的内容,
当您单击 imageButton 时,您可以通过以下方式获取它:
If your code generates the imageButtons then, in this code you can write something like,
and when your imageButton is clicked then you can get it with,
我必须做同样的事情,这就是我所做的
i had to do same thing and this is what i have done