Android commonly used Dialog box usage, androiddialog

Source: Internet
Author: User

Android commonly used Dialog box usage, androiddialog

Many Android versions usually use custom dialogs or V4 provided by Google during development,

The V7 compatibility package is developed to ensure the consistency of the dialog box style of each version. Therefore, AlertDialog in the V7 package is used here.

1 import android. app. progressDialog; 2 import android. content. dialogInterface; 3 import android. OS. bundle; 4 import android. OS. systemClock; 5 import android. support. v7.app. alertDialog; 6 import android. support. v7.app. appCompatActivity; 7 import android. view. view; 8 import android. widget. toast; 9 10 public class MainActivity extends AppCompatActivity {11 12 @ Override 13 protected void onCreate (Bund Le savedInstanceState) {14 super. onCreate (savedInstanceState); 15 setContentView (R. layout. activity_main); 16} 17 18/** 19 * normal dialog box 20*21 * @ param view 22 */23 public void click1 (View view) {24 AlertDialog. builder builder = new AlertDialog. builder (this); 25 // set the Title 26 builder. setTitle ("user security prompt:"); 27 // set the prompt message 28 builder. setMessage ("are you sure you want to accept the user's privacy terms? "); 29 builder. setPositiveButton ("OK", null); 30 builder. setNegativeButton ("cancel", null); 31 // display dialog 32 builder. show (); 33} 34 35/** 36 * single choice Dialog Box 37*38 * @ param view 39 */40 public void click2 (View view) {41 AlertDialog. builder builder = new AlertDialog. builder (this); 42 // set the title 43 builder. setTitle ("select a major:"); 44 // set the item entry 45 final String items [] = {"Java", "C ++ ", "PHP", "Android", "C #", "Python", "MySql"}; 46 // set the item Click Event 47 builder. setSingleChoiceItems (items,-1, new DialogInterface. onClickListener () {48 @ Override 49 public void onClick (DialogInterface dialog, int which) {50 Toast. makeText (MainActivity. this, "you have selected:" + items [which], Toast. LENGTH_SHORT ). show (); 51 dialog. dismiss (); 52} 53}); 54 // display dialog 55 builder. show (); 56} 57 58/** 59 * multiple choice dialog box 60*61 * @ param view 62 */63 public void click3 (View view) {64 AlertDialog. builder builder = new AlertDialog. builder (this); 65 // set the title 66 builder. setTitle ("select your hobbies:"); 67 // set the item entry 68 final String items [] = {"reading", "playing games ", "watching movies", "playing basketball", "Fitness", "self-driving tour", "going to the bar "}; 69 // set the selection status of each entry to "false". If "true" is not selected, 70 final boolean [] checks = {true, true, false, false} is selected }; 71 // set the boolean value 72 builder in checks when the item click event entry is selected. setMultiChoiceItems (items, checks, new DialogInterface. onMultiChoiceClickListener () {73 @ Override 74 public void onClick (DialogInterface dialog, int which, boolean isChecked) {75 checks [which] = isChecked; 76} 77 }); 78 // set the OK button and click event 79 builder. setPositiveButton ("OK", new DialogInterface. onClickListener () {80 @ Override 81 public void onClick (DialogInterface dialog, int which) {82 StringBuffer buffer = new StringBuffer (); 83 for (int I = 0; I <checks. length; I ++) {84 if (checks [I]) {85 buffer. append (items [I] + ","); 86} 87} 88 Toast. makeText (MainActivity. this, buffer. toString (), Toast. LENGTH_SHORT ). show (); 89} 90}); 91 builder. setNegativeButton ("cancel", null); 92 // display dialog 93 builder. show (); 94} 95 96/** 97 * progress bar dialog box 98*99 * @ param view100 */101 public void click4 (View view) {102 final ProgressDialog dialog = new ProgressDialog (this); 103 dialog. setTitle ("Please wait:"); 104 // dialog. setMessage ("loading... "); 105 // dialog. show (); 106 107 // do not set the progress bar style. The default value is circular 108 109 // set the maximum progress value to 100110 dialog. setMax (100); 111 // set the progress bar style to horizontal 112 dialog. setProgressStyle (ProgressDialog. STYLE_HORIZONTAL); 113 114 dialog. show (); 115 // simulate a progress loading process 116 new Thread () {117 @ Override118 public void run () {119 for (int I = 0; I <= 100; I ++) {120 dialog. setProgress (I); 121 // sleep 200 ms 122 SystemClock. sleep (200); 123} 124 dialog. dismiss (); 125} 126 }. start (); 127} 128}

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.