前段时间做项目,有个搜索功能,点击搜索按钮时,如果为空会有Toast提示, 但是测试人员疯狂的连续点击按钮,Toast提示框长时间存在后才失去(点击几次,就会出现几次),唉 ! 测试的伤不起啊!!!
现在说下怎样在连续点击情况下:Toast能够快速的失去
public class ToastUtil {
private static Handler handler = new Handler(Looper.getMainLooper());
private static Toast toast = null;
private static Object synObj = new Object();
private static View view;
public static void showMessage(final Context context, final String msg) {
showMessage(context, msg, Toast.LENGTH_SHORT);
}
public static void showMessage(final Context act, final String msg,
final int len) {
new Thread(new Runnable() {
public void run() {
handler.post(new Runnable() {
@Override
public void run() {
synchronized (synObj) {
if (toast != null) {
// toast.cancel();
toast.setView(view);
toast.setText(msg);
toast.setDuration(len);
} else {
toast = Toast.makeText(act, msg, len);
view = toast.getView();
}
toast.show();
}
}
});
}
}).start();
}
}
ok !!! 大功告成。。。
有什么不对的地方,请朋友批评指点,共同学习 。。。。。