Show Run Progress dialog box
We often have this experience: when running an application. You need to wait a while, and a progress (Please wait) dialog box is displayed to let the user know that the operation is in progress.
We continue to add code to the program in the previous article ~
1, add a button in the Activity_main.xml file of the previous article, add the following code for example:
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:orien tation= "vertical" android:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_ Horizontal_margin "android:paddingright=" @dimen/activity_horizontal_margin "android:paddingtop=" @dimen/activity_ Vertical_margin "tools:context=". Mainactivity "> <button android:id=" @+id/btn_dialog "android:layout_width=" Fill_parent "Android oid:layout_height= "Wrap_content" android:onclick= "OnClick" android:text= "Click to display a dialog"/> <button android:id= "@+id/btn_dialog2" android:layout_width= "Fill_parent" android:layout_height= "W Rap_content "android:onclick=" OnClick2 "android:text=" Click to display a progress dialog "/></linearla Yout>
2. Add a OnClick2 () method to the Mainactivity.java. The code blocks added are as follows:
public void OnClick2 (View v) {//---Show the dialog---final progressdialog dialog = Progressdialog.show (this, "Doing Someth ing "," Please wait ... ", true);//Create a Progress dialog box new thread (new Runnable () {////create a thread thread with Runnable code block @overridepublic void The code in the Run () {//run () method will run in a separate thread//TODO auto-generated method stubtry {//---simulate doing something Lengthy---thread.sleep (5000)//Simulate a 5-second operation//---Dismiss the dialog---dialog.dismiss ();//5 seconds later, call the dismiss method to close the progress dialog box} catch (Interruptedexception e) {//Todo:handle exceptione.printstacktrace ();}}). Start ();}
3, run. Click a second button. Effects such as the following:
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvemvuywlsntaxmti5/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center "/>
After 5 seconds, the progress bar itself to disappear, the program to restore the original state ~
Click to download the full code ~
Android Learning Note (eight)--Show Run Progress dialog box