以下代码用于 ImageButton 在每次点击时更改其图像。我已经创建了一个循环来改变它的位置,但它改变得太快了。
所以我需要一个延迟函数。我试过this solution ,但它对我不起作用。
It says "Handler is abstract and cannot be instantiated"
代码:
public void ShapeSelectingInGame() {
ShapeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ShapeButton = (ImageButton) v;
selectShape = rand.nextInt(4);
ShapeSaying = rand.nextInt(8);
ColorOfShape = rand.nextInt(10);
shapeID = "shape_" + selectShape + ShapeSaying + ColorOfShape;
resID = getResources().getIdentifier(shapeID, "drawable", "com.example.asgames.hitit");
ShapeButton.setImageResource(resID);
HitTypeString.setVisibility (View.INVISIBLE);
}
});
for (int i = 10; i < 10000; i += 100)
{
ShapeButton.setX(i);
}
ShapeButton.setVisibility(View.VISIBLE);
}
最佳答案
使用handler.postDelay
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
// your code here
}
}, 1000);
其中 1000 表示 1 秒
关于android - 如何为 setOnClickListener 设置延迟?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43389534/