This article summarizes the usage methods of Android 8 dialog boxes (Dialogs) and shares them for your reference. The specific content is as follows
1. Write it in front
Android provides a rich Dialog functions. This article introduces the 8 most commonly used methods of using dialog boxes, including ordinary (including prompt messages and buttons), lists, single-choice, multiple-choice, waiting, progress bar, editing, customization and other forms. It will be introduced in Part 2.
Sometimes, we want to complete some specific functions when the dialog is created or closed, which requires rewriting the create(), show(), dismiss() and other methods of Dialog, which will be introduced in Part 3.
2. Code examples
2.1 Normal Dialog (Figure 1 and Figure 2)
2 buttons
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); Button buttonNormal = (Button) findViewById(.button_normal); (new () { @Override public void onClick(View v) { showNormalDialog(); } }); } private void showNormalDialog(){ /* @setIcon Set dialog box icon * @setTitle Set dialog title * @setMessage Settings dialog message prompt * setXXX method returns a Dialog object, so properties can be set in chain */ final normalDialog = new (); (.icon_dialog); ("I'm a regular Dialog") ("Which button do you want to click?"); ("Sure", new () { @Override public void onClick(DialogInterface dialog, int which) { //...To-do } }); ("closure", new () { @Override public void onClick(DialogInterface dialog, int which) { //...To-do } }); // show (); } }
3 buttons
/* @setNeutralButton Set the middle button * If you only need one button, just set setPositiveButton */ private void showMultiBtnDialog(){ normalDialog = new (); (.icon_dialog); ("I'm a regular Dialog").setMessage("Which button do you want to click?"); ("Button 1", new () { @Override public void onClick(DialogInterface dialog, int which) { // ...To-do } }); ("Button 2", new () { @Override public void onClick(DialogInterface dialog, int which) { // ...To-do } }); ("Button 3", new () { @Override public void onClick(DialogInterface dialog, int which) { // ...To-do } }); // Create an instance and display it (); }
2.2 List Dialog (Figure 3)
private void showListDialog() { final String[] items = { "I'm 1","I'm 2","I'm 3","I'm 4" }; listDialog = new (); ("I'm a list Dialog"); (items, new () { @Override public void onClick(DialogInterface dialog, int which) { // which subscript starts from 0 // ...To-do (, "You clicked" + items[which], Toast.LENGTH_SHORT).show(); } }); (); }
2.3 Single-choice Dialog (Figure 4)
int yourChoice; private void showSingleChoiceDialog(){ final String[] items = { "I'm 1","I'm 2","I'm 3","I'm 4" }; yourChoice = -1; singleChoiceDialog = new (); ("I'm a single choice Dialog"); // The second parameter is the default option, which is set to 0 here (items, 0, new () { @Override public void onClick(DialogInterface dialog, int which) { yourChoice = which; } }); ("Sure", new () { @Override public void onClick(DialogInterface dialog, int which) { if (yourChoice != -1) { (, "You chose" + items[yourChoice], Toast.LENGTH_SHORT).show(); } } }); (); }
2.4 Multiple Select Dialog (Figure 5)
ArrayList<Integer> yourChoices = new ArrayList<>(); private void showMultiChoiceDialog() { final String[] items = { "I'm 1","I'm 2","I'm 3","I'm 4" }; // Set the default selected options, all false are not selected by default final boolean initChoiceSets[]={false,false,false,false}; (); multiChoiceDialog = new (); ("I'm a multi-choice Dialog"); (items, initChoiceSets, new () { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { if (isChecked) { (which); } else { (which); } } }); ("Sure", new () { @Override public void onClick(DialogInterface dialog, int which) { int size = (); String str = ""; for (int i = 0; i < size; i++) { str += items[(i)] + " "; } (, "You selected" + str, Toast.LENGTH_SHORT).show(); } }); (); }
2.5 Wait for Dialog (Figure 6)
private void showWaitingDialog() { /* Wait for Dialog to have the ability to block other controls * @setCancelable To make the screen non-clickable, set to non-cancel (false) * After the download and other events are completed, the function is actively called to close the Dialog */ ProgressDialog waitingDialog= new ProgressDialog(); ("I'm a waiting Dialog"); ("Waiting..."); (true); (false); (); }
2.6 Progress Bar Dialog (Figure 7)
private void showProgressDialog() { /* @setProgress Set initial progress * @setProgressStyle Set style (horizontal progress bar) * @setMax Set the maximum progress value */ final int MAX_PROGRESS = 100; final ProgressDialog progressDialog = new ProgressDialog(); (0); ("I'm a progress bar Dialog"); (ProgressDialog.STYLE_HORIZONTAL); (MAX_PROGRESS); (); /* Simulate the process of increasing progress * A new thread is opened, each 100ms, and the progress is increased by 1 */ new Thread(new Runnable() { @Override public void run() { int progress= 0; while (progress < MAX_PROGRESS){ try { (100); progress++; (progress); } catch (InterruptedException e){ (); } } // After the progress reaches the maximum value, the window disappears (); } }).start(); }
2.7 Edit Dialog (Figure 8)
private void showInputDialog() { /*@setView Load an EditView */ final EditText editText = new EditText(); inputDialog = new (); ("I'm an input Dialog").setView(editText); ("Sure", new () { @Override public void onClick(DialogInterface dialog, int which) { (, ().toString(), Toast.LENGTH_SHORT).show(); } }).show(); }
2.8 Custom Dialog (Figure 9)
<!-- res/layout/dialog_customize.xml--> <!-- CustomizeView --> <LinearLayout xmlns:andro android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android: android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> private void showCustomizeDialog() { /* @setView Load custom View ==> .dialog_customize * Since dialog_customize.xml only places an EditView, it is the same as in Figure 8 * dialog_customize.xml can customize more complex Views */ customizeDialog = new (); final View dialogView = () .inflate(.dialog_customize,null); ("I'm a custom Dialog"); (dialogView); ("Sure", new () { @Override public void onClick(DialogInterface dialog, int which) { // Get the input content in EditView EditText edit_text = (EditText) (.edit_text); (, edit_text.getText().toString(), Toast.LENGTH_SHORT).show(); } }); (); }
3. Rewrite the callback function
/* Rewrite the create and show functions of Builder, and you can implement necessary settings before the Dialog is displayed. * For example, initialization list, default options, etc. * @create Called on the first creation * @show called every time it is displayed */ private void showListDialog() { final String[] items = { "I'm 1","I'm 2","I'm 3","I'm 4" }; listDialog = new (){ @Override public AlertDialog create() { items[0] = "I'm No.1"; return (); } @Override public AlertDialog show() { items[1] = "I'm No.2"; return (); } }; ("I'm a list Dialog"); (items, new () { @Override public void onClick(DialogInterface dialog, int which) { // ...To-do } }); /* @setOnDismissListener Called when Dialog is destroyed * @setOnCancelListener Called when Dialog is closed */ (new () { public void onDismiss(DialogInterface dialog) { (getApplicationContext(), "Dialog is destroyed", Toast.LENGTH_SHORT).show(); } }); (); }
The above is all about this article, I hope it will be helpful to everyone's learning.