在点击处理程序中,您可以更改颜色,删除所有按钮的onclick监听器,并启动一个后台线程来计时。当后台线程睡眠2分钟后,您可以为所有按钮添加onclick监听器。这样你就不会创建一个完全没有响应的用户界面,但你可以达到禁用按钮的效果。
public class myListener implements OnClickListener() {
private boolean ignoreClicks = false;
public void setIgnoreClicks( boolean b ) {
this.ignoreClicks = b;
}
@Override
public void onClick( View v ) {
if ( !ignoreClicks ) {
// use v to get the button, then change the color
ignoreClicks = true;
// start a new AsyncTask and give it the listener and the view
// in doInBackground sleep for 2 seconds
// in onPostExecute change the color of the button back to normal
// (you have a reference to the button because you gave the view to the AsyncTask)
// and set ignoreClicks to false in the listener
}
}
}
创建此侦听器的一个实例,并将其用于每个按钮。