Use of Android AlertDialog dialogue panel—Android development

1 AlertDialog.Builder builder=new AlertDialog.Builder(this);
2 builder.setTitle("History").setIcon(R.mipmap.info).setMessage(str)
3.setPositiveButton("ok", new DialogInterface.OnClickListener() {
4 @Override
5 public void onClick(DialogInterface dialog, int which) {
6}
7 });
8 AlertDialog ad=builder.create();
9 ad.show();

The result displayed by the above code is as shown in the figure.

Briefly record the usage of AlertDialog:

The following content is compiled from: https://blog.csdn.net/streate/article/details/90899515

1. General prompt dialog box

It mainly contains the prompt title, the message body, and buttons such as “Cancel” and “OK” at the bottom.

 1 /**
 2 * Prompt dialog box
 3*/
 4 public void tipDialog() {
 5 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
 6 builder.setTitle("Prompt: ");
 7 builder.setMessage("This is a normal dialog box,");
 8 builder.setIcon(R.mipmap.ic_launcher);
 9 builder.setCancelable(true); //Click outside the dialog box to make the dialog box disappear
10
11 //Set the front button
12 builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
13 @Override
14 public void onClick(DialogInterface dialog, int which) {
15 Toast.makeText(MainActivity.this, "You clicked OK", Toast.LENGTH_SHORT).show();
16 dialog.dismiss();
17}
18 });
19 //Set the reverse button
20 builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
21 @Override
22 public void onClick(DialogInterface dialog, int which) {
23 Toast.makeText(MainActivity.this, "You clicked Cancel", Toast.LENGTH_SHORT).show();
24 dialog.dismiss();
25}
26 });
27 //Set neutral button
28 builder.setNeutralButton("Secret", new DialogInterface.OnClickListener() {
29 @Override
30 public void onClick(DialogInterface dialog, int which) {
31 Toast.makeText(MainActivity.this, "You chose neutral", Toast.LENGTH_SHORT).show();
32 dialog.dismiss();
33}
34 });
35
36
37 AlertDialog dialog = builder.create(); //Create AlertDialog object
38 //Listening events displayed in the dialog box
39 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
40 @Override
41 public void onShow(DialogInterface dialog) {
42 Log.e(TAG, "Dialog box is displayed");
43}
44 });
45 //Listening event for dialog box disappearing
46 dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
47 @Override
48 public void onCancel(DialogInterface dialog) {
49 Log.e(TAG, "The dialog box disappeared");
50 }
51 });
52 dialog.show(); //Show dialog box
53}
Methods used

– setTitle: Set the title of the dialog box, such as “Prompt”, “Warning”, etc.;
– setMessage: Set the specific information to be conveyed in the dialog box;
– setIcon: Set the icon of the dialog box;
– setCancelable: Whether clicking outside the dialog box will make the dialog box disappear, the default is true;
– setPositiveButton: Set the positive button, which means “positive” and “confirm”. The first parameter is the text displayed on the button, the same below;
– setNegativeButton: Set the negative button to mean “negative”, “deny”, and “cancel”;
– setNeutralButton: Set the neutral button;
– setOnShowListener: event triggered when the dialog box is displayed;
– setOnCancelListener: event triggered when the dialog box disappears.

2. Common list dialog box

The content of the list dialog box is a column of display content.

You need to use the setItems method of the constructor.

The first parameter is the list data, and the second parameter is the click listening interface.

We want to implement such a small function. When the user clicks on an item, a Toast will pop up to prompt the content of the selected item.

 1 /**
 2 * List dialog box
 3*/
 4 private void itemListDialog() {
 5 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
 6 builder.setTitle("Choose your favorite course:");
 7 builder.setCancelable(true);
 8 final String[] lesson = new String[]{"Chinese", "Mathematics", "English", "Chemistry", "Biology", "Physics", "Physical Education"};
 9 builder.setIcon(R.mipmap.ic_launcher);
10 builder.setIcon(R.mipmap.tab_better_pressed)
11.setItems(lesson, new DialogInterface.OnClickListener() {
12 @Override
13 public void onClick(DialogInterface dialog, int which) {
14 Toast.makeText(getApplicationContext(), "You chose" + lesson[which], Toast.LENGTH_SHORT).show();
15}
16 }).create();
17 //Set the front button
18 builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
19 @Override
20 public void onClick(DialogInterface dialog, int which) {
21 dialog.dismiss();
twenty two         }
twenty three     });
24 //Set the reverse button
25 builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
26 @Override
27 public void onClick(DialogInterface dialog, int which) {
28 dialog.dismiss();
29 }
30 });
31 AlertDialog dialog = builder.create(); //Create AlertDialog object
32 dialog.show(); //Show dialog box
33}

3. Radio selection dialog box

The content of the radio selection dialog box is a single selection list.

You need to use the setSingleChoiceItems method. The first parameter is list data.

The second parameter is the item selected by default, and the third parameter is the click monitoring interface.

We want to implement such a small function. After the user selects an item, he will record his choice.

This item will be selected by default the next time you click on the dialog box.

 1 /**
 2 * Radio selection dialog box
 3*/
 4 public void singleChoiceDialog() {
 5 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
 6
 7 builder.setTitle("Your current place of residence is:");
 8 final String[] cities = {"Beijing", "Shanghai", "Guangzhou", "Shenzhen", "Hangzhou", "Tianjin", "Chengdu"};
 9  
10 builder.setSingleChoiceItems(cities, chedkedItem, new DialogInterface.OnClickListener() {
11 @Override
12 public void onClick(DialogInterface dialog, int which) {
13 Toast.makeText(getApplicationContext(), "You chose" + cities[which], Toast.LENGTH_SHORT).show();
14 chedkedItem = which;
15}
16 });
17 builder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
18 @Override
19 public void onClick(DialogInterface dialog, int which) {
20 dialog.dismiss();
twenty one         }
twenty two     });
twenty three  
24 builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
25 @Override
26 public void onClick(DialogInterface dialog, int which) {
27 dialog.dismiss();
28 }
29 });
30
31 AlertDialog dialog = builder.create(); //Create AlertDialog object
32 dialog.show(); //Show dialog box
33}

4. Check dialog box

The check dialog box is a list that can be selected repeatedly, somewhat similar to the radio selection dialog box.

However, the setMultiChoiceItems method is called, and there is an additional Boolean parameter isChecked.

Indicates whether the currently clicked item is selected.

We create a collection and add the clicked item to the collection.

If you uncheck it, it will be removed from the collection. Click the confirm button to display the selected content.

 1 /**
 2 * Check dialog box
 3*/
 4 public void multiChoiceDialog() {
 5 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
 6 builder.setTitle("Please choose your favorite color:");
 7 final String[] colors = {"red", "orange", "yellow", "green", "blue", "indigo", "purple"};
 8 final List<String> myColors = new ArrayList<>();
 9  
10 builder.setMultiChoiceItems(colors, null, new DialogInterface.OnMultiChoiceClickListener() {
11 @Override
12 public void onClick(DialogInterface dialog, int which, boolean isChecked) {
13 if (isChecked) {
14 myColors.add(colors[which]);
15 } else {
16 myColors.remove(colors[which]);
17}
18}
19 });
20
21 builder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
22 @Override
23 public void onClick(DialogInterface dialog, int which) {
24 String result = "";
25 for (String color : myColors) {
26 result + = color + ",";
27}
28 Toast.makeText(getApplicationContext(), "You selected: " + result, Toast.LENGTH_SHORT).show();
29 dialog.dismiss();
30}
31 });
32
33 builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
34 @Override
35 public void onClick(DialogInterface dialog, int which) {
36 myColors.clear();
37 dialog.dismiss();
38}
39 });
40 AlertDialog dialog = builder.create(); //Create AlertDialog object
41 dialog.show(); //Show dialog box
42}
syntaxbug.com © 2021 All Rights Reserved.