As a developer, we need to constantly think about the problem from the user's perspective, for example, when the App store downloads software, when the user clicks on the download button, there will be download progress prompts the page appears, now we through the thread hibernation way to simulate download progress update demo, such as figure (here for screenshots to facilitate the setting of the dialogue progress bar located above the screen):
Layout Interface code (deploy only one button):
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
android:orientation=" vertical "android:layout_width=" match_parent "
android:layout_height=" Match_parent ">
<button
android:layout_width=" wrap_content "
android:layout_height=" Wrap_ Content "
android:text=" download//real project It is recommended that the uniform definition of text resources be configured in Strings.xml under
res android:onclick= "Begin"/>
< /linearlayout>
Java code implementation (simulated download progress update via thread):
public class Progressbardemo extends appcompatactivity {@Override protected void Oncrea
Te (@Nullable Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.progressbar);} public void Begin (View v) {//Materialized Progress Bar dialog box (progressdialog) Final progressdialog pd = new ProgressDialog (this); Pd.settitle ("please
Slightly wait ");
Sets the dialog progress bar style to horizontal pd.setprogressstyle (progressdialog.style_horizontal);
Set hint information pd.setmessage ("in desperate downloading ...");
Set the dialog progress bar to appear at the top of the screen (for easy screenshots) Pd.getwindow (). setgravity (Gravity.top);
Pd.setmax (100); Pd.show ()//Call Show method to display progress Bar Dialog///use Anonymous inner class to implement thread and start new thread (new Runnable () {int initial = 0;//initial download Progress @Override public void R Un () {while (Initial<pd.getmax ()) {//Set loop condition Pd.setprogress (initial+=40);/set to complete every try {thread.sleep (1000); catch (Interruptedexception e)
{E.printstacktrace ();}
Pd.dismiss ()///The dialog disappears when progress completes}). Start (); }
}
The above is a small set of Android introduced by the implementation of the Thread Update ProgressDialog (Dialog progress bar), I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!