我想按下一个按钮,它会改变按钮的颜色并播放声音,然后等待时间 (t),然后再次改变按钮的颜色。
button1.setOnClickListener(new View.OnClickListener(){
@Override
public void OnClick(View v) {
button1.setImageResource(R.drawable.yellow_block); /* Changes Color */
sp.play(sound,1,1,0,0,1); /*Plays sound*/
//Wait t amount of time here
button1.setImageResource(R.drawable.green_block); /* Changes Color Again */
最佳答案
你可以像这样使用postDelayed()
方法
button1.setOnClickListener(new View.OnClickListener(){
@Override
public void OnClick(View v) {
button1.setImageResource(R.drawable.yellow_block); /* Changes Color */
sp.play(sound,1,1,0,0,1); /*Plays sound*/
//Wait t amount of time here
button1.postDelayed(new Runnable() {
public void run() {
//Do what you want
button1.setImageResource(R.drawable.green_block); /* Changes Color Again */
}
},3*1000 /* This would be the milisecond you want to wait */);
}
}
如果你也想停止音乐,你可能需要稍微更改我的代码。
关于android - (Android) 按钮单击后等待更改图像按钮图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14092464/