通过它的 id 将图像按钮设置为 8x8 2d 数组.安卓.沙马林 [英] Setting Image Button via it's id into a 8x8 2d array. Android. Xamarin

查看:20
本文介绍了通过它的 id 将图像按钮设置为 8x8 2d 数组.安卓.沙马林的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我分别制作了大小为 8 和 8 的二维数组,并且制作了 64 个图像按钮,每个按钮都有一个 b1、b2、b3 id.我很想将 id 设置到数组中.

I have made a 2d array of size 8 and 8 respectively, and i have made 64 image buttons, each with a b1, b2 b3 id. I would love to set the id into the array.

一个例子是:

buttons[0, 0] = FindViewById(Resource.Id.b1);

buttons[0, 0] = FindViewById(Resource.Id.b1);

按钮[0, 1] = FindViewById(Resource.Id.b2);
.

buttons[0, 1] = FindViewById(Resource.Id.b2);
.

.

.

buttons[1, 0] = FindViewById(Resource.Id.b9);

buttons[1, 0] = FindViewById(Resource.Id.b9);

既然我有 64 个图像按钮,我是否必须写 64 行来初始化每个要解析为 2d 按钮数组的 Resource.Id?我知道我可以为每一行和每一列做一个双循环,例如:

Since I have 64 image button, do i have to write 64 lines to initialize each Resource.Id to be parsed into the 2d button array? I know I can do a double for loop for each row and column such as:

buttons[x, y] = FindViewById(Resource.Id.b1);

buttons[x, y] = FindViewById(Resource.Id.b1);

但是我必须硬编码要指定的图像按钮的 resource.id.有什么办法可以缩短这个?谢谢

However I have to hardcode the resource.id of the image button to specify. Any way to shorten this? Thanks

推荐答案

是的,您可以动态获取按钮的 ID 并填充您的数组:

Yes, you can get the ID of the button dynamicaly and fill your array:

    int overallCounter = 1;
    for (int i = 0 i < 8; i ++) {
       for (int j = 0;j < 8; j++ ) {
           buttons[i, j] = FindViewById(Resources.GetIdentifier("b" + overallCounter, "id", this.PackageName));
           overallCounter++;
       }
    }

这篇关于通过它的 id 将图像按钮设置为 8x8 2d 数组.安卓.沙马林的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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