试图依次显示多条Toast消息,但仅显示最后一条Toast消息。我尝试使用Thread.sleep和处理程序来缓冲消息,但是都没有用。还有其他提示吗?这是我的代码:
public class GenericExceptionHandlerFilter implements Filter {
private final List<HttpMessageConverter<?>> converters;
public GenericExceptionHandlerFilter(<HttpMessageConverter<?>> converters) {
this.converters = converters;
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
FilterChain filterChain) throws IOException {
try {
filterChain.doFilter(servletRequest, servletResponse);
} catch (Exception e) {
MyCommonResponse commonResponse = new MyCommonResponse(e);
write(servletRequest, servletResponse, comonResponse);
log.error("Unhandled exception.", e);
}
}
private void write(HttpServletRequest servletRequest, HttpServletResponse servletResponse, Object object) throws IOException {
String accept = servletRequest.getHeader("accept");
for (HttpMessageConverter messageConverter : converters) {
if (messageConverter.canWrite(object.getClass(), MediaType.valueOf(accept))) {
HttpOutputMessage outputMessage = new ServletServerHttpResponse(servletResponse);
messageConverter.write(object, MediaType.valueOf(accept), outputMessage);
}
}
}
}
答案 0 :(得分:2)
至少您应该在“ showToast”上添加一个“延迟”变量,并将Toast
调用移到发布的Runnable
内的 中:
private fun showToast(text: String, delayInMilliseconds: Int) {
val context = this
handler.postDelayed (
{ Toast.makeText(context, text, Toast.LENGTH_SHORT).show() },
delayInMilliseconds
)
}
那你会喜欢它
showToast(cowName.toString(), 0)
showToast(cowWeight.toString(), 1000)
showToast(cowSex.toString(), 2000)
showToast(cowAge.toString(), 3000)
或执行该工作所需的任何延迟值。