如何通过给定的 ID 调用视图

发布于 2025-01-02 14:32:38 字数 233 浏览 2 评论 0原文

我有一个创建 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

没︽人懂的悲伤 2025-01-09 14:32:38

如果您设置 ID,则您知道 ID。因此,您可以使用您设置的 ID 调用 findViewById()

IOW,如果您调用:

button.setId(14);

您稍后调用:

findViewById(14);

当然,欢迎您将 Button 对象保存在数组或其他内容中并以这种方式访问​​它们。

If you are setting the ID, you know the ID. Hence, you call findViewById() with the ID you set.

IOW, if you called:

button.setId(14);

you later call:

findViewById(14);

Of course, you are welcome to hold your Button objects in an array or something and access them that way.

心清如水 2025-01-09 14:32:38

一种方法是:每次创建按钮时,将其引用添加到容器对象中,例如:

Map<Integer, ImageView> buttonViews = new HashMap<Integer, ImageView>();

添加按钮状态:

ImageView b = new ImageView (this);
buttonViews.put(id, b);

更改任何按钮的属性:

buttonViews.get(id).setImageResource(R.drawable.buttonImage1);

通过这种方式,您可以制作各种效果...

One way to do this: each time you create a button add its reference into a container object like:

Map<Integer, ImageView> buttonViews = new HashMap<Integer, ImageView>();

To add a button state:

ImageView b = new ImageView (this);
buttonViews.put(id, b);

To change property to any button:

buttonViews.get(id).setImageResource(R.drawable.buttonImage1);

In this way you can make various effects...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文