简体   繁体   中英

How to make an AlertDialog disappear?

I am trying 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.

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

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, eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM