How to make an AlertDialog disappear?
I am t开发者_StackOverflow社区rying to integrate Twitter with my application and I thought to add an AlertDialog which will appear after users press a button and this AlertDialog will ask the users if they want to post a tweet(The tweet will be the same for all users, like a promo tweet). My question is if there is any way to make the Alert Dialog disappear after a few moments, like 5 seconds.
I found this but I am not completely sure how it works. I would appreciate any help.
Declare your Handler
that will deal to dismiss the dialog after some time, and declare thread that will pause the current thread and keep dialog for a some amount of time. In the Runnable
of the thread make a statement that will sleep the thread for a some time, e.g Thread.sleep(5000);
then use the Handler object to sendMessage()
;, in the Handler onHandleMessage()
method just do dialog.dissmiss();
After some operation, at point where you want keep the dialog, and start the thread using myThread.start();
. Mind the instantiation of the thread each time when/where you need.
you cannot access to UI from other threads so you must use handler to make modification on UI from the thread.
this is the code you use to make dialog disappear:
d.dismiss();
and this is the handler code
Handler handler= new Handler()
use the method from link and send to it time and dialog object
精彩评论