Android ProgressBar progress bar and ProgressDialog progress box display demo_php tips

Source: Internet
Author: User
When doing mobile phone development, often run into some more time-consuming operations, this time the progress bar began to come in handy.
This demo shows the ProgressBar progress bar and the ProgressDialog progress box.
A, ProgressDialog progress box, the effect as shown in the picture:

The code is as follows:
Copy Code code as follows:

Progress dialog Box Button monitor
Class Prossbuttonlistener implements Onclicklistener {
@Override
public void OnClick (View v) {
Mydialog = Progressdialog.show (Progressbardemo.this, "progress bar title",
"Progress bar Content", true);

New Thread () {
public void Run () {
try {
/* Here Write the program snippet to run the background.
* * To see the effect, to suspend the 3 seconds as a demonstration * *
Sleep (3000);
catch (Exception e) {
E.printstacktrace ();
finally {
Uninstalls the Mydialog object that is created.
Mydialog.dismiss ();
}
}
}.start (); /* Start running the running thread * *
}
}

Second, progress Bar dialog box, here in two situations to dynamically display progress bar scale
1, handle method
The effect chart is as follows:

The code is as follows:
Copy Code code as follows:

progress bar Handle Button monitor
Class Prossbarhandlebuttonlistener implements Onclicklistener {
@Override
public void OnClick (View v) {
Progressbarhandle.setvisibility (view.visible);
Myprossbarhandletext.setvisibility (view.visible);
Progressbarhandle.setmax (1000);
New Thread () {
public void Run () {
for (int i=0;i<=1000;) {
try {
/* Write the running progress bar here * *
msg = new Message ();
Msg.what = 1;
Msg.getdata (). Putint ("size", I);
Handler.sendmessage (msg);//handle send a message
* * To see the effect, to suspend the 1 seconds as a demonstration * *
Sleep (100);
i+=10;
catch (Exception e) {
Handler.obtainmessage ( -1). Sendtotarget ();
E.printstacktrace ();
}
}
}
}.start (); /* Start running the running thread * *
}
}

Handle receive messages
Private Handler Handler = new Handler () {

@Override
public void Handlemessage (msg) {
Switch (msg.what) {
Case 1:
Progressbarhandle.setprogress (Msg.getdata (). GETINT ("size"));
float num = (float) progressbarhandle.getprogress ()/(float) progressbarhandle.getmax ();
int result = (int) (NUM*100);
System.out.println ("progressbarhandle.getprogress () =======" +progressbarhandle.getprogress ());
Myprossbarhandletext.settext (result+ "%");
if (Progressbarhandle.getprogress () ==progressbarhandle.getmax ()) {
Toast.maketext (Progressbardemo.this, "Download Successful", 1). Show ();
Progressbarhandle.setvisibility (View.gone);
Myprossbarhandletext.setvisibility (View.gone);
}
Break

Case-1:
Toast.maketext (progressbardemo.this, "Download Failed", 1). Show ();
Break
}
}
};

2, the use of Asynctask method, the effect chart and handle effect as
The specific code is as follows:
Copy Code code as follows:

Progress bar Synctask Button Monitor
Class Prossbarsyncbuttonlistener implements Onclicklistener {
@Override
public void OnClick (View v) {
New Asyncloader (). Execute ((Void) null);
}
}


Asynctask Task Execution
Class Asyncloader extends Asynctask<void, Integer, integer> {
@Override
Executes before the Doinbackground method executes
protected void OnPreExecute () {
Progressbarhandle.setvisibility (view.visible);
Myprossbarhandletext.setvisibility (view.visible);
Progressbarhandle.setmax (100000);
}

Do a specific time consuming operation
Protected Integer doinbackground (Void ... params) {
This is displayed with a 10,000 progress bar scale.
int totalsize = 100000;
for (int i = 0; i < 100000;) {
Publishprogress (i); Passing data to Onprogressupdate method execution by pushing messages
i+=10;
}
return totalsize;
}
Executed during the Doinbackground method execution
protected void Onprogressupdate (Integer ... progress) {
Progressbarhandle.setprogress (Progress[0]);
float num = (float) progressbarhandle.getprogress ()/(float) progressbarhandle.getmax ();
int result = (int) (NUM*100);
Myprossbarhandletext.settext (result+ "%");
}

Executes after the Doinbackground method finishes
protected void OnPostExecute (Integer result) {
Toast.maketext (Progressbardemo.this, "Download successful, download" +result, 1). Show ();
Myprossbarhandletext.setvisibility (View.gone);
Progressbarhandle.setvisibility (View.gone);
}
}

Click to download demo sample

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.