Android-based AlertDialog application, androidalertdialog
Dialog Box dialog in Android
The dialog box is an essential component for applications. It is no exception in Android and is used to prompt important information...
Android provides rich Dialog Box support, which provides four Common Dialog Box formats:
- AlertDialog: warning dialog box, the most widely used and rich dialog box
- ProgressDialog: progress bar dialog box, which is a simple encapsulation of the progress bar
- DatePickerDialog: Date dialog box
- TimePickerDialog: Time dialog box
The following describes how to use AlertDialog:
Activity_main layout File
1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" 3 xmlns: tools = "http://schemas.android.com/tools" 4 android: layout_width = "match_parent" 5 android: layout_height = "match_parent" 6 android: paddingBottom = "@ dimen/activity_vertical_margin" 7 android: paddingLeft = "@ dimen/users" 8 android: paddingRight = "@ dimen/activity_horizontal_margin" 9 android: paddingTop = "@ dimen/activity_vertical_margin" 10 tools: context = "com. dragon. android. alertdialog. mainActivity "> 11 12 <Button13 android: layout_width =" wrap_content "14 android: layout_height =" wrap_content "15 android: text =" massage "16 android: id = "@ + id/bt_msg" 17 android: layout_alignParentTop = "true" 18 android: layout_centerHorizontal = "true"/> 19 20 <Button21 android: layout_width = "wrap_content" 22 android: layout_height = "wrap_content" 23 android: text = "sure" 24 android: id = "@ + id/bt_sure" 25 android: layout_below = "@ + id/bt_msg" 26 android: layout_alignRight = "@ + id/bt_msg" 27 android: layout_alignEnd = "@ + id/bt_msg"/> 28 29 <Button30 android: layout_width = "wrap_content" 31 android: layout_height = "wrap_content" 32 android: text = "EditText" 33 android: id = "@ + id/bt_editText" 34 android: layout_below = "@ + id/bt_sure" 35 android: layout_alignLeft = "@ + id/bt_sure" 36 android: layout_alignStart = "@ + id/bt_sure"/> 37 38 <Button39 android: layout_width = "wrap_content" 40 android: layout_height = "wrap_content" 41 android: text = "singlechoice" 42 android: id = "@ + id/bt_singleChoice" 43 android: layout_below = "@ + id/bt_editText" 44 android: layout_centerHorizontal = "true"/> 45 46 <Button47 android: layout_width = "wrap_content" 48 android: layout_height = "wrap_content" 49 android: text = "mutichoice" 50 android: id = "@ + id/bt_multiChoice" 51 android: layout_below = "@ + id/bt_singleChoice" 52 android: layout_centerHorizontal = "true"/> 53 54 <Button55 android: layout_width = "wrap_content" 56 android: layout_height = "wrap_content" 57 android: text = "imageview" 58 android: id = "@ + id/bt_imageView" 59 android: layout_below = "@ + id/bt_multiChoice" 60 android: layout_centerHorizontal = "true"/> 61 </RelativeLayout>Activity_main
MainActivity
1 package com. dragon. android. alertdialog; 2 3 import android. content. dialogInterface; 4 import android. OS. bundle; 5 import android. support. v7.app. alertDialog; 6 import android. support. v7.app. appCompatActivity; 7 import android. view. view; 8 import android. widget. editText; 9 import android. widget. imageView; 10 11 public class MainActivity extends AppCompatActivity implements View. onClickListener {12 13 @ Override14 protected void onCreate (Bundle savedInstanceState) {15 super. onCreate (savedInstanceState); 16 setContentView (R. layout. activity_main); 17 initView (); 18} 19 20 private void initView () {21 findViewById (R. id. bt_msg ). setOnClickListener (this); 22 findViewById (R. id. bt_sure ). setOnClickListener (this); 23 findViewById (R. id. bt_editText ). setOnClickListener (this); 24 findViewById (R. id. bt_singleChoice ). setOnClickListener (this); 25 findViewById (R. id. bt_multiChoice ). setOnClickListener (this); 26 findViewById (R. id. bt_imageView ). setOnClickListener (this); 27} 28 29 @ Override30 public void onClick (View v) {31 AlertDialog. builder builder = new AlertDialog. builder (this); 32 switch (v. getId ()){
33 case R. id. bt_msg: 34 builder35. setTitle ("Title") // set Title 36. setMessage ("this is a massage") // sets the message 37. setPositiveButton ("sure", null) // parameter 1: button name; parameter 2: Listener 38. show (); 39 break;
40 case R. id. bt_sure: 41 builder42. setTitle ("Sure") 43. setMessage ("Sure? ") 44. setPositiveButton (" sure ", null) 45. setNegativeButton (" cancel ", null) 46. show (); 47 break;
48 case R. id. bt_editText: 49 builder50. setTitle ("Please input") 51. setView (new EditText (this) // this method displays a View52. setPositiveButton ("sure", null) 53. setNegativeButton ("cancel", null) 54. show (); 55 break;
56 case R. id. bt_singleChoice: 57 builder58. setTitle ("Please choice") 59. setSingleChoiceItems (new String [] {"A", "B", "C"}, 0, 60 new DialogInterface. onClickListener () {61 @ Override62 public void onClick (DialogInterface dialog, int which) {63 // single button, click one to automatically exit 64 dialog. dismiss (); 65} 66}) // parameter 1: Number of options in the selection box; parameter 2: ID of the selected option; parameter 3: Listener 67. setNegativeButton ("cancel", null) 68. show (); 69 break;
70 case R. id. bt_multiChoice: 71 builder72. setTitle ("Please choice") 73. setMultiChoiceItems (new String [] {"A", "B", "C"}, null, null) // select 74 in the same category. setPositiveButton ("sure", null) 75. setNegativeButton ("cancel", null) 76. show (); 77 break;
78 case R. id. bt_imageView: 79 ImageView imageView = new ImageView (this); 80 imageView. setImageResource (R. mipmap. ic_launcher); // sets image resource 81 builder82. setTitle ("Image") 83. setView (imageView) // This method displays a View84. setPositiveButton ("sure", null) 85. show (); 86 break; 87} 88} 89}