机器人:如何优雅地设置多个按钮的ID [英] android: how to elegantly set many button IDs

查看:91
本文介绍了机器人:如何优雅地设置多个按钮的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的活动中许多按钮(只有一小部分,其中可见的时间)。目前,我有一些丑陋这样的:

I have many buttons in my activity (only a subset of which are visible at a time). I currently have something ugly like this:

buttonID[0] = R.id.buttonr1b1;
buttonID[1] = R.id.buttonr1b2;
buttonID[2] = R.id.buttonr1b3;
buttonID[3] = R.id.buttonr1b4;
...
buttonID[35] = R.id.buttonr1b36;

for (int i = 0; i < 36; i++) {
    button[i] = (Button) findViewById(buttonID[i]);
}

有没有更优雅的方式将所有的R.id.buttonXXX的?它只是看起来那么错误的和丑陋。 谢谢你。

Is there a more elegant way to reference all of R.id.buttonXXX ? It just looks so wrong and ugly. Thank you.

推荐答案

您的直觉是正确的。它的丑陋,一般如果你发现自己想要做到这一点,你应该重新考虑你的设计。

Your instincts are correct. It's ugly and in general if you find yourself wanting to do this you should rethink your design.

如果您的按钮是要遍历他们做这样的事情统一的地步,他们很可能不够均匀,产生编程摆在首位(你可以存储为你创建它们的引用)或使用某种形式的适配器视图的。需要哪些数据与每个按钮关联?您可以直接使用setTag / getTag关联呢?你需要在这里使用的ID呢?

If your buttons are uniform to the point where you want to loop over them to do something like this, they're probably uniform enough to generate programmatically in the first place (and you can store references as you create them) or use some form of AdapterView. What data needs to be associated with each button? Can you associate it directly using setTag/getTag? Do you need to use IDs here at all?

这篇关于机器人:如何优雅地设置多个按钮的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆