Android progress dialog box, android progress dialog box

Source: Internet
Author: User

Android progress dialog box, android progress dialog box

Layout file:

<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: orientation = "vertical" android: layout_width = "match_parent" android: layout_height = "match_parent" tools: context = ". dialogActivity "> <TextView android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: text =" @ string/text "/> <Button android: id = "@ + id/btn_dialog" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: onClick = "onClick" android: text = "Multi-choice dialog box"/> <Button android: id = "@ + id/btn_dialog1" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: onClick = "onClick1" android: text = "Progress dialog box"/> <Button android: id = "@ + id/btn_dialog2" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: onClick = "onClick2" android: text = "detailed progress dialog box"/> </LinearLayout>

DialogActivity. java

Package com. dialog; import android. app. activity; import android. app. alertDialog; import android. app. alertDialog. builder; 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 DialogActivity extends Activity {// CharSequence interface CharSequence [] items = {"google", "apple", "Baidu", "Microsoft"}; boolean [] itemsChecked = new boolean [items. length]; // create an instance of the ProgressDialog class ProgressDialog = null; protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main);} // override the onCreateDialog () method protected Dialog onCreateDialog (int id, Bundle args) {switch (id) {case 0: /** the sub-class of AlertDialog Dialog displays the progress indicator and the optional text message or view Dialog box. * Multiple choice dialog box */Builder builder = new AlertDialog. builder (this); builder. setIcon (R. drawable. ic_launcher); builder. setTitle ("which Internet company do you like"); // response confirmation button builder. setPositiveButton ("OK", new DialogInterface. onClickListener () {// Toast is not directly used in DialogActivity. Instead, it is used in the AlertDialog class. Therefore, you need to use getBaseContext () to return an instance of the Context class public void onClick (DialogInterface dialog, int whichButton) {Toast. makeText (getBaseContext (), "OK to press Button is clicked ", Toast. LENGTH_SHORT ). show () ;}}); // response to the cancel button builder. setNegativeButton ("cancel", new DialogInterface. onClickListener () {public void onClick (DialogInterface dialog, int whichButton) {Toast. makeText (getBaseContext (), "the cancel button is clicked", Toast. LENGTH_SHORT ). show () ;}}); // responds to the multi-choice event builder. setMultiChoiceItems (items, itemsChecked, new DialogInterface. onMultiChoiceClickListener () {public void onClick (DialogInt Erface dialog, int which, boolean isChecked) {Toast. makeText (getBaseContext (), (isChecked? "Selected": "Not selected") + items [which], Toast. LENGTH_SHORT ). show () ;}}); return builder. create (); case 1:/** set icon title style button * Show detailed progress dialog box */progressDialog = new ProgressDialog (this); progressDialog. setIcon (R. drawable. ic_launcher); progressDialog. setTitle ("download file... "); progressDialog. setProgressStyle (ProgressDialog. STYLE_HORIZONTAL); progressDialog. setButton (DialogInterface. BUTTON_POSITIVE, "OK", new DialogInterfac E. OnClickListener () {public void onClick (DialogInterface dialog, int which) {Toast. makeText (getBaseContext (), "OK clicked! ", Toast. LENGTH_SHORT ). show () ;}}); progressDialog. setButton (DialogInterface. BUTTON_NEGATIVE, "Cancel", new DialogInterface. onClickListener () {public void onClick (DialogInterface dialog, int which) {Toast. makeText (getBaseContext (), "Cancel clicked! ", Toast. LENGTH_SHORT ). show () ;}}); return progressDialog;} return null;} public void onClick (View v) {// call the onCreateDialog () method showDialog (0 );} /** progress dialog box */public void onClick1 (View v) {final ProgressDialog dialog = ProgressDialog. show (this, "Please wait 3 seconds", "Please wait... ", true); // start a new Thread (new Runnable () {public void run () {try {Thread. sleep (3000); // remove the dialog box from the screen. This method is thread-safe. dismiss ();} catch (InterruptedException e) {e. printStackTrace ();}}}). start ();}/** detailed progress dialog box */public void onClick2 (View v) {showDialog (1); // set the initial value to 0 progressDialog. setProgress (0); new Thread (new Runnable () {public void run () {for (int I = 1; I <= 15; I ++) {try {Thread. sleep (1000); // update dialog box progressDialog. incrementProgressBy (int) 100/15);} catch (InterruptedException e) {e. printStackTrace () ;}} progressDialog. dismiss ();}}). start ();}}


In Android development, the circular progress bar and Layout in the dialog box

First, solve problem 1:
You must have used the oncreateDialog and showdialog methods of the system, so this will display the second time but will not be displayed. You should call the show () method in the dialog method to display the information, in this way, the progress bar is displayed in a circle every time.

Question 2:
You have set a white background, but there is a box below, but I have a question: If you set it to a white background, if your forwarding record is white by default, won't you see it? Okay, this problem is not tangled! The black one is caused by the border. just remove the border.

Generally, we use custom dialog, that is, to write a class to inherit the dialog. At this time, the constructor is:

Public MyDialog (Context context, int theme ){
Super (context, theme );
This. context = context;
Init ();
}

What is theme? The style is as follows:

<Resources> <style name = "dialog" parent = "@ android: style/Theme. dialog "> <item name =" android: windowFrame "> @ null </item> <! -- Border -- <item name = "android: incluwisfloating"> true </item> <! -- Whether to float above the activity --
<Item name = "android: javaswistranslucent"> false </item> <! -- Translucent -- <item name = "android: windowNoTitle"> true </item> <! -- No title -- <item name = "android: windowBackground"> @ color/transparent </item> <! -- Background transparency -- <item name = "android: backgroundDimEnabled"> false </item> <! -- Fuzzy -- </style> </resources & gt ...... remaining full text>

In the android dialog box, add the "OK" and "cancel" buttons, but only one of them is displayed. Why?

Protected void dialog (){
AlertDialog. Builder builder = new Builder (Main. this );
Builder. setMessage ("are you sure you want to exit? ");
Builder. setTitle ("prompt ");
Builder. setPositiveButton ("OK", new OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){
Dialog. dismiss ();
Main. this. finish ();
}
});
Builder. setNegativeButton ("cancel", new OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){
Dialog. dismiss ();
}
});
Builder. create (). show ();
}

All of your connections are builder. setPositiveButton (). Of course, only one is displayed.

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.