我目前正在跟踪此问题:Setting Image Button via it's id into a 8x8 2d array. Android. Xamarin。
我已成功将64图像按钮的id设置为8x8阵列。在我的图像按钮中,按钮上有一些带有图像的按钮,代码如下:
android:src="@android:drawable/ic_menu_gallery"
android:background="@drawable/white"
还有其他图像按钮是空白的,没有图像。我的问题是:在我的所有64个图像按钮中,我想根据上面的代码计算有多少按钮背景图像为白色。
目前我有以下代码:
for (int i = 0; i < 8; i++) //Rows of the array "buttons"
{
for (int j = 0; j < 8; j++) //Column of the array "buttons"
{
if(buttons[i, j] == ....???) //Check if the id of the image button has a white drawable image.
}
}
我应该输入什么来完成代码?
答案 0 :(得分:2)
在C#.NET中,我尝试将这些信息放在Tags中。要在Android中使用Tags,您必须在values / Strings.xml中创建String资源。这用于确保标记是唯一的。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="ColorOfButton">color</string>
</resources>
初始化按钮时,您可以创建如下标记:
buttons[i, j].SetTag(Resource.String.ColorOfButton, "white");
//sets it to white
或者用XML:
<Button
android:id="@+id/testBtn">
<tag android:id="@string/ColorOfButton" android:value="white" />
</Button>
<Button
android:id="@+id/testBtn2">
<tag android:id="@string/ColorOfButton" android:value="black" />
</Button>
然后你可以得到标签,看看它是白色,黑色还是空白:
switch(buttons[i, j].GetTag(Resource.String.ColorOfButton).ToString())
{
case "white":
//Your picture is white
break;
case "black":
//Your picture is black
break;
case "empty":
//Your picture is empty
break;
}
您可以在标记中添加以下字符串:
白色 - 白色片
黑色 - 黑色片段
空 - 无件