Android (5) Wait window, android wait window

Source: Internet
Author: User

Android (5) Wait window, android wait window

I. Wait for window implementation:

Record: sometimes it is necessary to add a waiting window to improve the user experience when downloading data. There are two waiting windows:

The first type of rotating ProgressBar:

Main Interface:

Public class MainActivity extends Activity {private Button button1; private TextView textview1; public AlertDialog selfdialog; private MainActivity main; public Handler mhandler = new Handler () {public void handleMessage (Message msg) {switch (msg. what) {// display TextViewcase 1: Toast. makeText (main, "successful", Toast. LENGTH_SHORT ). show (); textview1.setVisibility (View. VISIBLE); break;} super. handleMessage (msg) ;}}; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); main = this; button1 = (Button) findViewById (R. id. button1); textview1 = (TextView) findViewById (R. id. textview1); button1.setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View arg0) {showDialogLoading () ;});} // wait for your void showDialogLoading () {LayoutInflater inflater = (LayoutInflater) this. getSystemService (this. LAYOUT_INFLATER_SERVICE); // viewView view = inflater. inflate (R. layout. footer, null); AlertDialog. builder ad = new AlertDialog. builder (this); selfdialog = ad. create (); selfdialog. setView (view); // block click selfdialog. setCancelable (false); selfdialog. show (); new Thread () {@ Overridepublic void run () {try {Thread. sleep (3000);} catch (InterruptedException e) {e. printStackTrace ();} Message message = new Message (); message. what = 1; main. mhandler. sendMessage (message); // disable dialogselfdialog. dismiss ();}}. start ();}}

Activity_main.xml:

<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: orientation = "vertical" android: gravity = "center"> <Button android: id = "@ + id/button1" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Click me"/> <TextView android: id = "@ + id/textview1" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "show me after loading" android: visibility = "gone"/> </LinearLayout>

Footer. xml:

<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: orientation = "vertical" android: gravity = "center"> <Button android: id = "@ + id/button1" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Click me"/> <TextView android: id = "@ + id/textview1" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "show me after loading" android: visibility = "gone"/> </LinearLayout>

In this way, the first wait window is implemented.


The second type of progress bar wait window:



Main Interface:

Public class MainActivity extends Activity {private Button button1; private TextView textview1; private TextView textview2; public AlertDialog selfdialog; private MainActivity main; public Handler mhandler = new Handler () {public void handleMessage (Message msg) {switch (msg. what) {// display TextViewcase 1: textview2.setText ("being downloaded (" + msg. obj. toString () + "/10)"); break;} super. handleMessage (msg) ;}}; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); main = this; button1 = (Button) findViewById (R. id. button1); textview1 = (TextView) findViewById (R. id. textview1); button1.setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View arg0) {showDialogLoading () ;});} // wait for your void showDialogLoading () {LayoutInflater inflater = (LayoutInflater) this. getSystemService (this. LAYOUT_INFLATER_SERVICE); // viewView view = inflater. inflate (R. layout. footer, null); final ProgressBar progressbar1 = (ProgressBar) view. findViewById (R. id. progressbar1); textview2 = (TextView) view. findViewById (R. id. textview2); AlertDialog. builder ad = new AlertDialog. builder (this); selfdialog = ad. create (); selfdialog. setView (view); // block click selfdialog. setCancelable (false); selfdialog. show (); new Thread () {@ Overridepublic void run () {// getProgress () Get the current progress value // incrementProgressBy () increase each time // getMax () obtain the maximum range of this progress bar int max = progressbar1.getMax (); int I = 0; while (true) {if (I> max) {break;} try {Thread. sleep (1000); progressbar1.incrementProgressBy (1); Message message = new Message (); message. what = 1; message. obj = progressbar1.getProgress (); main. mhandler. sendMessage (message); I ++;} catch (InterruptedException e) {e. printStackTrace () ;}/// disable dialogselfdialog. dismiss ();}}. start ();}}

Footer. xml:

<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: orientation = "vertical" android: gravity = "center"> <ProgressBar android: id = "@ + id/progressbar1" style = "? Android: attr/progressBarStyleHorizontal "android: layout_width =" 200dip "android: layout_height =" wrap_content "android: max =" 10 "/> <TextView android: id = "@ + id/textview2" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Download in progress (0/10)" android: textColor = "@ android: color/black" android: textSize = "9sp"/> </LinearLayout>

Activity_main has the same layout as the previous example.



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.