Following the implementation of the Click button to display the loading box, 2 seconds after the automatic disappearance.
1. First add a button to the layout xml:
<Button Android:id="@+id/button1"Android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:layout_alignparentleft="true"Android:layout_alignparenttop="true"Android:layout_marginleft="32DP"Android:layout_margintop="14DP"Android:background="@drawable/button_style"Android:text="@string/btntext"/>
2, add the View.onclicklistener event in the background Java code, rewrite the onclick, the code is as follows:
Processbutton = (Button) Findviewbyid (r.id.button1); Processbutton.setonclicklistener (myonclicklister);
View.onclicklistener Myonclicklister =NewView.onclicklistener () { Public voidOnClick (View v) {final ProgressDialog prodialog= Android.app.ProgressDialog.show (mainactivity. This,"Test","automatically disappears after 2 seconds! "); Thread Thread=NewThread () { Public voidrun () {Try{sleep ( -); } Catch(interruptedexception e) {//TODO Auto-generated catch blockE.printstacktrace (); } Prodialog.dismiss ();//This sentence must not be less, otherwise the program will be stuck dead. } }; Thread.Start (); } };
3, so the use of the thread of the sleep method to achieve. It is important to note, however, that the dismiss () method of the dialog object must be called at the end of the thread, or the program will go into the dead loop. The card died there.
4, the effect came out, is this:
Apps for Android in ProgressDialog