如何通过给定的 ID 调用视图
我有一个创建 64 个按钮的循环,在循环内,按钮使用 button.setId(n)
获取一个 id
问题是,如何调用具有特定 id 的按钮来更改其特性。
理想情况下,我正在寻找类似于此的东西
ImageView button2 = (ImageView)findViewById(button.("with id 14, for example"))
I have a loop that creates 64 buttons, and within the loop, the button gets an id using button.setId(n)
The question is, how do I call the button with a certain id to change its properties.
Ideally, I'm looking for something similar to this
ImageView button2 = (ImageView)findViewById(button.("with id 14, for example"))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您设置 ID,则您知道 ID。因此,您可以使用您设置的 ID 调用
findViewById()
。IOW,如果您调用:
您稍后调用:
当然,欢迎您将
Button
对象保存在数组或其他内容中并以这种方式访问它们。If you are setting the ID, you know the ID. Hence, you call
findViewById()
with the ID you set.IOW, if you called:
you later call:
Of course, you are welcome to hold your
Button
objects in an array or something and access them that way.一种方法是:每次创建按钮时,将其引用添加到容器对象中,例如:
添加按钮状态:
更改任何按钮的属性:
通过这种方式,您可以制作各种效果...
One way to do this: each time you create a button add its reference into a container object like:
To add a button state:
To change property to any button:
In this way you can make various effects...