ImageButtons 数组,从变量分配 R.view.id

发布于 2024-09-27 08:41:23 字数 1388 浏览 8 评论 0原文

嘿。我的应用程序将使用 64 个 ImageButtons (8x8) 的数组,它们都已在我的 XML 布局中声明,名称如 one1、two5、eight8 等。我认为,与其在 Java 中单独声明它们,不如在一些 for 循环中声明它们可能是明智的。 然后我

ImageButton musicGrid[][] = new ImageButton [8][8];

有嵌套的 for 循环,它基本上创建了一个字符串来代替 R.id.whatever。这只是我循环中的最后一行,应该进行分配。正确的语法是什么,或者这甚至不可能做到(如果是这样,我如何更好地处理 64 个按钮网格?)。谢谢!

for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
            String btnID = "R.id.";
            switch(i) {
            case 0: btnID.concat("one"); break;
            case 1: btnID.concat("two"); break;
            case 2: btnID.concat("three"); break;
            case 3: btnID.concat("four"); break;
            case 4: btnID.concat("five"); break;
            case 5: btnID.concat("six"); break;
            case 6: btnID.concat("seven"); break;
            case 7: btnID.concat("eight"); break;
            }
            switch(j) {
            case 0: btnID.concat("1"); break;
            case 1: btnID.concat("2"); break;
            case 2: btnID.concat("3"); break;
            case 3: btnID.concat("4"); break;
            case 4: btnID.concat("5"); break;
            case 5: btnID.concat("6"); break;
            case 6: btnID.concat("7"); break;
            case 7: btnID.concat("8"); break;
            }
            musicGrid[i][j] = (ImageButton) findViewById(btnID);
        }
    }

Hey there. My app is going to be using an array of 64 ImageButtons (8x8), and they're all already declared in my XML Layout with names like one1, two5, eight8, etc. Rather than declare these each individually in my Java I thought it might be smart to declare them all in some for loops. I have

ImageButton musicGrid[][] = new ImageButton [8][8];

Then I have my nested for loops that basically create a string that will be in place of R.id.whatever. It's just that last line in my loops, that is supposed to do the assigning. What would the correct syntax for that be, or is this not even possible to do (and if so, how better would I handle a 64 button grid?). Thanks!

for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
            String btnID = "R.id.";
            switch(i) {
            case 0: btnID.concat("one"); break;
            case 1: btnID.concat("two"); break;
            case 2: btnID.concat("three"); break;
            case 3: btnID.concat("four"); break;
            case 4: btnID.concat("five"); break;
            case 5: btnID.concat("six"); break;
            case 6: btnID.concat("seven"); break;
            case 7: btnID.concat("eight"); break;
            }
            switch(j) {
            case 0: btnID.concat("1"); break;
            case 1: btnID.concat("2"); break;
            case 2: btnID.concat("3"); break;
            case 3: btnID.concat("4"); break;
            case 4: btnID.concat("5"); break;
            case 5: btnID.concat("6"); break;
            case 6: btnID.concat("7"); break;
            case 7: btnID.concat("8"); break;
            }
            musicGrid[i][j] = (ImageButton) findViewById(btnID);
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

朦胧时间 2024-10-04 08:41:23

我喜欢AndrewKS的for,它更优雅。请记住,findViewById 接收的是整数而不是字符串。所以你必须做类似的事情:

int resID = getResources().getIdentifier(btnID, "drawable", "com.your.package");
musicGrid[i][j] = (ImageButton) findViewById(resID);

I like AndrewKS' for, it's more elegant. Just keep in mind that findViewById receives an integer rather than a String. So you will have to do something like:

int resID = getResources().getIdentifier(btnID, "drawable", "com.your.package");
musicGrid[i][j] = (ImageButton) findViewById(resID);
黑色毁心梦 2024-10-04 08:41:23

如果您还没有对 xml 中的按钮进行硬编码,我会说使用 ViewInflater 以编程方式执行此操作,但既然您这样做了,那么代码如下:

String[] number_as_word = ["one", "two", "three", "four", "five", "six", "seven", "eight"];
for (int i = 0; i < 8; i++) {
  for (int j = 0; j < 8; j++) {
    musicGrid[i][j] = (ImageButton) findViewById("R.id." + number_as_word[i] + (j+1));
  }
}

If you didn't already hardcode the buttons in the xml, I would have said to do it programmatically with a ViewInflater, but since you did here's the code:

String[] number_as_word = ["one", "two", "three", "four", "five", "six", "seven", "eight"];
for (int i = 0; i < 8; i++) {
  for (int j = 0; j < 8; j++) {
    musicGrid[i][j] = (ImageButton) findViewById("R.id." + number_as_word[i] + (j+1));
  }
}
愿与i 2024-10-04 08:41:23

除非有一些特定需要将其作为单独的 ImageButtons 执行,否则您最好使用 网格视图

这里是使用适配器在 GridView 中使用图像的教程。

Unless there's some specific need to do it as individual ImageButtons, you might be better off using a GridView.

Here is a tutorial using images in a GridView using an adapter.

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