Android Custom ProgressDialog Progress Bar dialog box usage detailed _android

Source: Internet
Author: User

Basic usage of ProgressDialog

ProgressDialog is the progress dialog box. The Android phone comes with a simple dialog box that we can use to define what we want to show in the dialog box ProgressDialog.

First look at the ProgressDialog inside the method

Setprogressstyle: Set the progress bar style, the style is round, rotate.

SETTITLT: Setting the title
Setmessage: Set the hint information;
SetIcon: Set the caption icon;
Setindeterminate: Sets whether the ProgressDialog progress bar is ambiguous; This property has no practical meaning for the Progressdailog default runner mode, which is set to True by default. It is only useful for dialog with ProgressBar. After you modify this property to False, you can update the progress bar in real time.

Setcancelable: Sets the ProgressDialog whether can press the return key to cancel;
Cancellistner: The current dialog will be executed after it is forced to cancel and is usually used to clean up unfinished tasks.

Setbutton: Sets a button for the ProgressDialog (the button event needs to be monitored);

Show: Shows ProgressDialog.

Cancel: Delete ProgressDialog

Dismiss: Delete ProgressDialog effect and cancel same

Setprogress (intcounter), update progress bar, of course, generally require a combination of handler to update the progress bar
<!--endfragment-->

And then we look at the effect

The implementation code is as follows

<span style= "" >package cn.android;
Import android.app.Activity;
Import Android.app.ProgressDialog;
Import Android.content.DialogInterface;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.os.Handler;
Import Android.os.Message;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.Toast;
public class Secondactivity extends activity implements runnable{
/**
* Called when the activity is the created.
* Activity Entrance
* */
Private button b_dialog,b_dialog1,button;//two buttons
Private ProgressDialog pd,pd1;//Progress Bar dialog box
private int count;
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.second);//Association corresponding interface
B_dialog = (Button) This.findviewbyid (R.id.button_dialog);
B_dialog1 = (Button) This.findviewbyid (R.ID.BUTTON_DIALOG1);
Handle what happens when an event occurs
B_dialog.setonclicklistener (listener);
B_dialog1.setonclicklistener (listener);
</span>
<span style= "" >}
Defining Listener Objects
Private Onclicklistener listener = new Onclicklistener () {
After the mouse is pressed
public void OnClick (View v) {
The id--type that gets the currently triggered event is int
int id = V.getid ();
if (id = = R.id.button_dialog) {
Process dialog box that will disappear when you press the OK key
PD = new ProgressDialog (secondactivity.this);//Create ProgressDialog Object
Pd.setprogressstyle (Progressdialog.style_spinner)//Set the progress bar style, the style is round, rotate
Pd.settitle ("hint");//Set ProgressDialog title
Pd.setmessage ("This is a circular progress bar dialog box");//SET ProgressDialog hint information
Pd.seticon (R.drawable.icon);//Set ProgressDialog header icon
Set whether the progress bar for progressdialog is ambiguous or not set to ambiguous
Pd.setindeterminate (FALSE);
Pd.setcancelable (TRUE); Sets whether ProgressDialog can be canceled by pressing the return key
Pd.setbutton ("OK", New Bt1dialoglistener ()); Set a button for ProgressDialog
Pd.show (); Let ProgressDialog show
In 1 seconds, the Process dialog box will disappear.
A different dialog box pops up
PD = Progressdialog.show (secondactivity.this, "Auto Close Dialog", "Working,,,,,, 1 Seconds", true, false);
Thread thread = new Thread (secondactivity.this);/Start a thread delay
Thread.Start ()//Start thread
}else if (id = = R.ID.BUTTON_DIALOG1) {
PD1 = new ProgressDialog (secondactivity.this);//Create ProgressDialog Object
Pd1.setprogressstyle (progressdialog.style_horizontal)//Set the progress bar style, the style is round, rotate
Pd1.settitle ("hint");//Set ProgressDialog title
Pd1.setmessage ("This is a Bar progress bar dialog box");//SET ProgressDialog hint info
Pd1.seticon (r.drawable.secondback);//Set ProgressDialog header icon
Set whether the progress bar for progressdialog is ambiguous or not set to ambiguous
Pd1.setindeterminate (FALSE);
Pd1.setcancelable (TRUE); Sets whether ProgressDialog can be canceled by pressing the return key
Pd1.setprogress (100)//Set ProgressDialog progress bar Progress
Pd1.show (); Let ProgressDialog show
Count = 0;
New Thread () {
public void Run () {
try {
while (Count <= 100) {
To control progress by thread
Pd1.setprogress (count++);
Thread.Sleep (100);
}
Pd1.cancel ();
catch (Exception e) {
Pd1.cancel ();
}
}
}.start ();
}
}
};
Run is to implement
public void Run () {
try {
Thread.Sleep (1000);/sleep 1 seconds
catch (Interruptedexception e) {
E.printstacktrace ();
}
Handler.sendemptymessage (0);//Send Message
}
Define objects to process messages
Private Handler Handler = new Handler () {
Join the news, that's it.
public void Handlemessage (msg) {
Pd.dismiss ()//Dialog disappears
Toast.maketext (Secondactivity.this, "the dialog box disappears", 3). Show ();
}
};
Listener class for PdButton01
Class Bt1dialoglistener implements Dialoginterface.onclicklistener {
@Override
public void OnClick (Dialoginterface dialog, int which) {
Click "OK" button to cancel the dialog box
Dialog.cancel ();
}
}
}
</span>

Related Article

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.