大家好这是我的第一篇文章,所以请温柔。即使我的 android final 已经完成,我仍然觉得很难不继续调整我的程序并添加到它只是为了它的乐趣。它是一个简单的程序,当您触摸屏幕时,屏幕上的角色会以两种方式中的一种笑,并带有匹配的振动。它工作得很好,但是当我的老师(当时心情不好)去测试它时,他几乎把按钮捣碎了,这使它每次按下按钮都排成一列,我们不得不像 15 次笑声一样坐下来,然后我们才能用手机做更多事情。我想要做的只是有一个触摸事件计数,直到第一个事件完成。它是一个简单的触摸事件,带有几个嵌套的 if 语句。
public boolean onTouch(final View v, MotionEvent m)
{
v.setBackgroundResource(R.drawable.laughing);//changes the image to the laughing monkey
if (m.getAction() == MotionEvent.ACTION_DOWN)
{
if (timesTouched != I) //checks to see if the touch amount is not equal to the random number
{
if(laughter != 0)
{
sp.play(laughter, 1, 1, 1, 0, 1);//plays the loaded sound when the screen is pressed
//vib.vibrate(900);
}
Time = 900;
timesTouched++;
intDelay = 1;
if(vibon == 1)
{
vib.vibrate(Time);
}
}
else if (timesTouched == I)//checks to see if the touch amount is the same as the random number
{
if(laughter != 0)
{
sp.play(laughFit, 1, 1, 1, 0, 1);//plays the loaded sound when the screen is pressed
}
Time = 6000;
timesTouched = 0;
intDelay = 1;
if(vibon == 1)
{
vib.vibrate(laugh, -1);
}
}
}
else if((m.getAction() == MotionEvent.ACTION_UP) && (intDelay == 1))
{
try {
Thread.sleep(Time);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
v.setBackgroundResource(R.drawable.normal) ;//returns the image to the normal looking monkey
intDelay = 0;
}
return true;
}
}
睡眠定时器可以防止背景图像在笑声结束之前恢复到默认值。我确实试图让我的老师帮忙,但他只是一个快速的替代品,在今年年初开始教书之前,他甚至从未接触过安卓设备。请您提供的任何帮助将不胜感激,因为到目前为止,我不得不在 Google 搜索的帮助下自学这些东西。
谢谢一堆!