The use of Android ProgressDialog

Source: Internet
Author: User
<span id="Label3"></p><p><p>ProgressDialog inherits from Alertdialog,alertdialog inherits from dialog, implements Dialoginterface Interface.</p></p><p><p>There are two ways to create progressdialog, one is new Dialog, and the other is a static method called Dialog Dialog.show ().</p></p><pre><pre><span style="color: #008000;">//</span> <span style="color: #008000;">mode one: new Dialog </span> <span style="color: #0000ff;">Final</span> <span style="color: #0000ff;">New</span> ProgressDialog (<span style="color: #0000ff;"></span>this<span style="color: #000000;">); Dialog.show (); <br><br></span></pre></pre><pre><pre><span style="color: #008000;">//</span> <span style="color: #008000;">mode two: Create and display statically, this progress bar can only be a circle bar, set the title and message prompt content</span> ProgressDialog dialog2 = progressdialog.show (<span style="color: #0000ff;"></span> this<br><br></pre></pre><pre><pre><span style="color: #008000;">//</span> <span style="color: #008000;">mode three is created and displayed statically, and this progress bar can only be a circular bar, where the last parameter, boolean indeterminate, is set to </span> an ambiguous state ProgressDialog Dialog3 =<span style="color: #000000;"> progressdialog.show (</span><span style="color: #0000ff;"></span>this<span style="color: #0000ff;">false</span>); <br><br></pre></pre><pre><pre><span style="color: #008000;">//</span> <span style="color: #008000;">mode four is created and displayed statically, this progress bar can only be a circle bar, here the last parameter Boolean cancelable set whether the progress bar can be canceled</span> ProgressDialog dialog4 = progressdialog.show (<span style="color: #0000ff;"></span>this, "prompt", "logging in"<span style="color: #000000;"></span><span style="color: #0000ff;">false</span><span style="color: #0000ff;">True </span>); <br><br></pre></pre><pre><pre><span style="color: #008000;">//</span> <span style="color: #008000;">mode Five is created and displayed in a static way, this progress bar can only be a circular bar, here the last parameter Dialoginterface.oncancellistener</span> <span style="color: #008000;">//</span> <span style="color: #008000;">Cancellistener for listening progress bar canceled </span> ProgressDialog dialog5 = progressdialog.show (<span style="color: #0000ff;"></span>This<span style="color: #0000ff;">true</span><span style="color: #000000;"></span><span style="color: #0000ff;">true</span>, cancellistener); <br><br></pre></pre><p><p>The way v needs a cancellistener, the code is as Follows:</p></p><pre><pre><span style="color: #0000ff;">Private</span> <span style="color: #0000ff;">New</span> <span style="color: #000000;">oncancellistener () {@Override </span>public<span style="color: #0000ff;"></span><span style="color: #0000ff;">void</span> <span style="color: #000000;"> onCancel (dialoginterface Dialog) { </span> <span style="color: #008000;">//</span>  <span style="color: #008000;">TODO auto-generated Method stub</span> Toast.maketext (mainactivity. <span style="color: #0000ff;">  </span>this, "progress Bar is canceled"<span style="color: #000000;">, toast.length_long). show (); }};</span></pre></pre><p><p></p></p><p><p><strong>There are two types of progressdialog, one is circular ambiguous state, the other is horizontal progress bar state</strong></p></p><p><p><span style="font-size: 14pt;">First Way: round progress bar</span></p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span>ProgressDialog dialog =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>ProgressDialog (<span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span><span style="color: #000000;"><span style="color: #000000;">); Dialog.setprogressstyle (progressdialog.style_spinner);</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">set the progress bar in the form of a circular rotating progress bar</span></span>Dialog.setcancelable (<span style="color: #0000ff;"><span style="color: #0000ff;">true</span></span>);<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">set Whether you can cancel by clicking</span> the back key</span>Dialog.setcanceledontouchoutside (<span style="color: #0000ff;"><span style="color: #0000ff;">false</span></span>);<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">set whether to cancel dialog progress bar outside of click Dialog</span></span>Dialog.seticon (r.drawable.ic_launcher);<span style="color: #008000;"><span style="color: #008000;">//</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Set the icon of the title of the hint, default is not, if not set the title, only set icon will not display</span></span>Dialog.settitle ("hint"<span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Dismiss monitoring</span></span>Dialog.setondismisslistener (<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Dialoginterface.ondismisslistener () {@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">Ondismiss (dialoginterface Dialog) {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Method Stub</span></span><span style="color: #000000;"><span style="color: #000000;"> } }); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">the <span style="color: #008000;">Listen key event is passed to dialog</span></span>Dialog.setonkeylistener (<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Dialoginterface.onkeylistener () {@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Boolean</span></span>OnKey (dialoginterface dialog,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">keycode, keyevent Event) { </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Method Stub</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">false</span></span><span style="color: #000000;"><span style="color: #000000;">; } }); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Monitoring the Cancel event</span></span>Dialog.setoncancellistener (<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Dialoginterface.oncancellistener () {@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">onCancel (dialoginterface Dialog) {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Method Stub</span></span><span style="color: #000000;"><span style="color: #000000;"> } }); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">set a clickable button up to three (by Default)</span></span>Dialog.setbutton (dialoginterface.button_positive, "ok"<span style="color: #000000;"><span style="color: #000000;">, </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Dialoginterface.onclicklistener () {@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>OnClick (dialoginterface dialog,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">Which) { </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Method Stub</span></span><span style="color: #000000;"><span style="color: #000000;"> } }); Dialog.setbutton (dialoginterface.button_negative,</span></span>Cancel<span style="color: #000000;"><span style="color: #000000;">, </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Dialoginterface.onclicklistener () {@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>OnClick (dialoginterface dialog,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">Which) { </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Method Stub</span></span><span style="color: #000000;"><span style="color: #000000;"> } }); Dialog.setbutton (dialoginterface.button_neutral,</span></span>Neutral<span style="color: #000000;"><span style="color: #000000;">, </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Dialoginterface.onclicklistener () {@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>OnClick (dialoginterface dialog,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">Which) { </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Method Stub</span></span><span style="color: #000000;"><span style="color: #000000;"> } }); Dialog.setmessage (</span></span>"this is a circular progress bar"<span style="color: #000000;"><span style="color: #000000;">); Dialog.show (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Thread (<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Runnable () {@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">Run () {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Method Stub</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Try</span></span><span style="color: #000000;"><span style="color: #000000;">{thread.sleep (</span></span>5000<span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">the <span style="color: #008000;">Cancel and dismiss methods are essentially the same, all from the screen to remove the dialog, the only difference is</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">calling the Cancel method will callback Dialoginterface.oncancellistener if registered, the dismiss method will not return</span></span><span style="color: #000000;"><span style="color: #000000;">Dialog.cancel (); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Dialog.dismiss ();</span></span>}<span style="color: #0000ff;"><span style="color: #0000ff;">Catch</span></span><span style="color: #000000;"><span style="color: #000000;">(interruptedexception E) {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Catch block</span></span><span style="color: #000000;"><span style="color: #000000;">E.printstacktrace (); }}). Start ();</span></span></pre><p><p>The background operation is simulated by Thread.Sleep (5000).</p></p><p><p>The Cancel and dismiss methods are essentially the same, all from the screen to remove dialog, the only difference Is: call the Cancel method will callback Dialoginterface.oncancellistener if registered, The dismiss method does not fall Back.</p></p><p><p></p></p><p><p><span style="font-size: 14pt;"><strong>Second Way: horizontal progress bar</strong></span></p></p><pre> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">the <span style="color: #008000;">progress bar also has the form of a two-level progress bar, which does not demonstrate</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span>ProgressDialog dialog =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>ProgressDialog (<span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span><span style="color: #000000;"><span style="color: #000000;">); Dialog.setprogressstyle (progressdialog.style_horizontal);</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">set the horizontal progress bar</span></span>Dialog.setcancelable (<span style="color: #0000ff;"><span style="color: #0000ff;">true</span></span>);<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">set Whether you can cancel by clicking</span> the back key</span>Dialog.setcanceledontouchoutside (<span style="color: #0000ff;"><span style="color: #0000ff;">false</span></span>);<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">set whether to cancel dialog progress bar outside of click Dialog</span></span>Dialog.seticon (r.drawable.ic_launcher);<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">set the icon for the title of the prompt, which is not the default</span></span>Dialog.settitle ("hint"<span style="color: #000000;"><span style="color: #000000;">); Dialog.setmax (</span></span>100<span style="color: #000000;"><span style="color: #000000;">); Dialog.setbutton (dialoginterface.button_positive,</span></span>Determine<span style="color: #000000;"><span style="color: #000000;">, </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Dialoginterface.onclicklistener () {@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>OnClick (dialoginterface dialog,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">Which) { </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Method Stub</span></span><span style="color: #000000;"><span style="color: #000000;"> } }); Dialog.setbutton (dialoginterface.button_negative,</span></span>Cancel<span style="color: #000000;"><span style="color: #000000;">, </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Dialoginterface.onclicklistener () {@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>OnClick (dialoginterface dialog,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">Which) { </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Method Stub</span></span><span style="color: #000000;"><span style="color: #000000;"> } }); Dialog.setbutton (dialoginterface.button_neutral,</span></span>Neutral<span style="color: #000000;"><span style="color: #000000;">, </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Dialoginterface.onclicklistener () {@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>OnClick (dialoginterface dialog,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">Which) { </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Method Stub</span></span><span style="color: #000000;"><span style="color: #000000;"> } }); Dialog.setmessage (</span></span>"this is a horizontal progress bar"<span style="color: #000000;"><span style="color: #000000;">); Dialog.show (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Thread (<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Runnable () {@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">Run () {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Method Stub</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>i = 0<span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> while</span>(i < 100<span style="color: #000000;"><span style="color: #000000;">) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Try</span></span><span style="color: #000000;"><span style="color: #000000;">{thread.sleep (</span></span>200<span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">Update progress bar progress <span style="color: #008000;">, you can update progress bar progress in a child thread</span></span>Dialog.incrementprogressby (1<span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Dialog.incrementsecondaryprogressby (ten)</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">level two progress bar update mode</span></span>i++<span style="color: #000000;"><span style="color: #000000;">; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Catch</span></span><span style="color: #000000;"><span style="color: #000000;">(Exception E) {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Todo:handle Exception</span></span><span style="color: #000000;"><span style="color: #000000;"> } } </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Delete Dialog when the progress bar has gone through</span></span><span style="color: #000000;"><span style="color: #000000;">Dialog.dismiss (); }}). Start ();</span></span></pre><p><p></p></p><p><p>The use of Android ProgressDialog</p></p></span>

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.