I have a custom toast diplaying an image and some text. This toast is big enough to cover almost half of the screen. It has a duration of LENGTH_LONG because it contains much information.

In some cases this can be a bit annoying for the user. Is there any way to cancel the toast when the user clicks on it? If not, is there any way to cancel the toast when clicking anywhere outside the toast?

Thanks!

有帮助吗?

解决方案

Keep a reference of you Toast, you can then use the cancel() method, documentation here.

In order to know when you need to cancel the toast, you will need to intercept the touch event with the OnTouchEvent of your activity and get the position x/y of the touch event. Then you can compare it with myToast.getView().getY()and getX() to get the top left corner and getWidth() and getHeight().

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top