[Learn Android while working on projects] knowledge point: ProgressDialog and ProgressBar In the Android control series,

Source: Internet
Author: User

[Learn Android while working on projects] knowledge point: ProgressDialog and ProgressBar In the Android control series,
ProgressDialog

ProgressDialog and ProgressBar dynamically display a loading icon in the UI to display the running status.
ProgressDialog is inherited from Android. app. the Interactive dialog window designed by AlertDialog. during use, you must create a ProgressDialog object. A "dialog box" will pop up during running as a reminder, in this case, the background of the application loses focus (that is, the UI component cannot be operated at this time), and the control is not handed over to the application until the process ends. If you do not want the background to lose focus in the Activity, if you want to prompt the User that a background program is in a busy stage, the ProgressBar will be used.

ProgressDialog pd = new ProgressDialog (TestProgerssDialogActivity. this); // set the pd style. setProgress (ProgressDialog. STYLE_SPINNER); // circular pd. setProgress (ProgressDialog. STYLE_HORIZONTAL); // horizontal // set the pd title pd. setTitle ("prompt"); // set the pd prompt to pd. setMessage ("this is a circular progress bar dialog box"); // set the pd progress bar icon pd. setIcon (R. drawable. flag); // set whether the progress bar of the pd is unclear // when the current value is not rolled, it moves between the minimum and maximum values. Generally, it serves as a prompt when performing tasks that cannot determine the operation time, you can set the current progress value pd based on your progress. setIndeterminate (false); // pd. setProgress (m_count ++); // whether you can press the key to cancel the pd. setCancelable (true); // set a Buttonpd of the pd. setButton ("OK", new DialogInterface. onClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {dialog. cancel () ;}}); // displays the pd. show ();

When ProgressDialog is enabled, When you click back, ProgressDialog is canceled first. The above code can listen to the progress bar cancellation event (that is, click Back to cancel ProgressDialog ), at this time, you can cancel the background operations here.

ProgressBar

Important XML attributes

Android: progressBarStyle: Default progress bar style android: progressBarStyleHorizontal: horizontal style
Important Methods
GetMax (): Maximum range of the returned progress bar
GetProgress (): returns the progress
GetSecondaryProgress (): returns the secondary progress
IncrementProgressBy (int diff): Specifies the increase progress.
IsIndeterminate (): indicates whether the progress bar is in uncertain mode.
SetIndeterminate (boolean indeterminate): sets the uncertain mode.
SetVisibility (int v): sets whether the progress bar is visible.

Important events
OnSizeChanged (int w, int h, int oldw, int oldh): this event is triggered when the progress value changes.

 

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"      android:orientation="vertical"     android:layout_width="fill_parent"      android:layout_height="fill_parent">      <TextViewandroid:layout_width="fill_parent" android:layout_height="wrap_content"android:text="Welcome to blog" />      <ProgressBarandroid:id="@+id/rectangleProgressBar" style="?android:attr/progressBarStyleHorizontal" mce_style="?android:attr/progressBarStyleHorizontal"  android:layout_width="fill_parent"android:layout_height="wrap_content"android:visibility="gone"/>     <ProgressBarandroid:id="@+id/circleProgressBar" style="?android:attr/progressBarStyleLarge" mce_style="?android:attr/progressBarStyleLarge"android:layout_width="wrap_content"android:layout_height="wrap_content"android:visibility="gone"/>     <Button android:id="@+id/button"    android:text="Show ProgressBar"     android:layout_width="wrap_content"    android:layout_height="wrap_content"    /></LinearLayout>


Refer:

Http://www.jb51.net/article/38532.htm


Why do not call progressDialog in dialog in android to load progressDialog simultaneously?

What do you want to say?
// ProgressDialog = ProgressDialog. show (daoru. this, "Loading...", "Please wait ...");
InitProgressDialog ();
Do these two sentences need not be used at the same time?
If you are asking this question, the answer is simple, because the two are the same and there is no need to repeat them. They both display a prompt box and enable the thread to load contacts.

Daxia's help: android and ProgressBar controls

This is in the main UI process. The pBar operations in your for loop will not be updated immediately. After the code is complete, the UI will be refreshed. The solution is to use Handler and Thread, I wrote a code snippet to help you:

Public class temp extends Activity {

Private ProgressBar pBar;

@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );

Button btn = (Button) findViewById (R. id. Button01 );
PBar = (ProgressBar) findViewById (R. id. ProgressBar01 );
PBar. setMax (100 );

Btn. setOnClickListener (new OnClickListener (){
Public void onClick (View v ){
MHandler. post (mThread );
V. setEnabled (false );
}
});
}

Private Handler mHandler = new Handler ();
Private Thread mThread = new Thread (){
Int I = 0;
@ Override
Public void run (){
MHandler. removeCallbacks (mThread );
PBar. setProgress (I );
I ++;
If (I <pBar. getMax ()){
MHandler. postDelayed (mThread, 100 );
}
}
};

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.