将视图的标记与整数进行比较

Ben*_*292 6 tags android integer compare

我正在尝试将整数数组与我唯一制作的imageviews标签进行比较.

使用这一行:

if(grid[i][j] == buttons[k].getTag()){
Run Code Online (Sandbox Code Playgroud)

我知道我在正确的轨道上,但我无法弄清楚我是否需​​要投射或使用方法.我知道这是一个简单的问题,但是非常感谢任何帮助,谢谢.

tde*_*aux 23

标签是一个对象,所以放一个Integer:

/*
 * UseValueOf
 * ----------
 * Priority: 4 / 10
 * Severity: Warning
 * Category: Performance
 * 
 * You should not call the constructor for wrapper classes directly, such as`new
 * Integer(42)`. Instead, call the valueOf factory method, such as
 * Integer.valueOf(42). This will typically use less memory because common
 * integers such as 0 and 1 will share a single instance.
 */
//MyView.setTag(new Integer(42));
MyView.setTag(Integer.valueOf(42));
Run Code Online (Sandbox Code Playgroud)

然后像这样检索值:

int tagValue = (Integer)MyView.getTag();
Run Code Online (Sandbox Code Playgroud)


小智 11

你必须转换整数按钮[k] .getTag().

做这个:

if(grid[i][j] == Integer.parseInt(buttons[k].getTag().toString())){
Run Code Online (Sandbox Code Playgroud)


use*_*917 4

我认为你的标签是一个字符串而不是一个整数。

如果是这种情况,请将您的 Integer 转换为 String() 并检查它是否 equals()。