My Android Notes (10)--progressdialog simple application, Waiting for hints (reprint)

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/barryhappy/article/details/7376231

In the application often use some time-consuming operation, need users to wait, such as loading Web content ...

At this point you need a hint to tell the user that the program is being executed, and that there is no animation or real death ... I'm so embarrassed ...

and ProgressBar, ProgressDialog and so on are specialized to do this.


Take ProgressDialog as an example, the general use of it is: before the time-consuming operation pops up ProgressDialog prompt the user, and then open a new thread, in the new thread to perform time-consuming operations, Notifies the main program to end ProgressDialog after execution is complete.

Here is a demo, very simple to use:

Package Com.android.ui;      Import android.app.Activity;      Import Android.app.ProgressDialog;      Import Android.os.Bundle;      Import Android.os.Handler;      Import Android.os.Message;      Import Android.view.View;      Import Android.view.View.OnClickListener;            Import Android.widget.Button;  Public classMainactivity extends Activity {Privatebutton button; PrivateProgressDialog PD; @Override Public voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);                    Setcontentview (R.layout.main); Button=(Button) Findviewbyid (R.id.button1); Button.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {/*Show ProgressDialog*/PD= Progressdialog.show (mainactivity. This,"title","load, please later ..."); /*start a new thread and execute a time-consuming method in a new thread*/                      NewThread (NewRunnable () {@Override Public voidrun () {Spandtimemethod ();//time-consuming approachHandler.sendemptymessage (0);//execute time-consuming method and send to Handler}}). Start ();                }              }); }                Private voidSpandtimemethod () {Try{Thread.Sleep ( the); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }} Handler Handler=NewHandler () {@Override Public voidHandlemessage (Message msg) {//handler will execute this method when it receives the messagePd.dismiss ();//Close ProgressDialog            }          }; }  

Main.xml only a button, do not post, the program should be well understood, click the button to pop up ProgressDialog, in the new thread to perform time-consuming operation (Thread.Sleep (5000);), after the completion of the notification handler, End ProgressDialog.

The results are as follows:

My Android Notes (10)--progressdialog simple application, Waiting for hints (reprint)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.