Android Hundred Day Program: Progress bar dialog box implementation

Source: Internet
Author: User
Tags deprecated

Displays the dialog managed by the activity.

This kind of dialog has a variety of, which is more common is loading time, the display of a loading progress bar.

Android displays such a progress bar is still very convenient, because there are ready-made modules can be called.

First look at the effect of this program:

1 Main interface:


2 Click this button to display:


Progress bar to 100 will automatically shut down, of course, here is the simulation download, the real download algorithm needs to continue to improve, but also very simple algorithm, not a difficult point.

Clicking the Cancel or OK button also invokes the function, making the desired action, which shows a toast (simple short-lived dialog) prompt. No.


Steps:

After you create a new project, use the following code to make the main interface:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" fill_parent "    android:layout_height=" fill_parent "    android:o rientation= "vertical" >    <button        android:id= "@+id/dialog_button" android:layout_width= "Wrap_        Content "        android:layout_height=" wrap_content "        android:layout_gravity=" center "        android:onclick=" OnClick "        android:text=" @string/dialog_button "/>  </LinearLayout>

Then the logic code:

Package Su.dialog.dialog;import Android.app.activity;import Android.app.dialog;import android.app.ProgressDialog; Import Android.content.dialoginterface;import Android.os.bundle;import Android.view.view;import Android.widget.toast;public class Mainactivity extends Activity {progressdialog progressdialog; @Overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);} public void OnClick (View v) {showDialog (1);p rogressdialog.setprogress (0), New Thread (new Runnable () {public void run () {  for (int i = 1; i <=; i++) {try {thread.sleep (+);p Rogressdialog.incrementprogressby ((int) (100.F/15.F));} catch (Interruptedexception e) {E.printstacktrace ();}} Progressdialog.dismiss ();}}). Start ();} @Overrideprotected Dialog oncreatedialog (int id) {ProgressDialog = new ProgressDialog (this);p Rogressdialog.seticon ( R.drawable.ic_launcher);p rogressdialog.settitle ("Downloading files ...");p Rogressdialog.setprogressstyle ( Progressdialog.sTyle_horizontal);p Rogressdialog.setbutton (dialoginterface.button_positive, "OK", new Dialoginterface.onclicklistener () {public void OnClick (dialoginterface dialog, int whichbutton) {Toast.maketext ( Getbasecontext (), "OK clicked!", Toast.length_short). Show ();}); Progressdialog.setbutton (Dialoginterface.button_negative, "Cancel", new Dialoginterface.onclicklistener () {public void OnClick (dialoginterface dialog, int whichbutton) {Toast.maketext (Getbasecontext (), "Cancel clicked!", Toast.length_short). Show ();}}); return progressdialog;}}

The ShowDialog () function is already a function of deprecated and can be referenced by: http://stackoverflow.com/questions/10285047/ Showdialog-deprecated-whats-the-alternative


Knowledge Points:

1 Click event is declared in XML, that is: Android:onclick = "onclick", the following "onclick" is a function of its own definition, can be any name, as long as the name of the XML and logic code in the same name can drive the click event.

2 Module ProgressDialog class is a bit complex, in fact, some of the properties of the settings and function calls, it is not difficult to understand, look at the code carefully.




Android Hundred Day Program: Progress bar dialog box implementation

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.