Android Studio 之 AlertDialog

 

• AlertDialog Profile

  • AlertDialog can pop up a dialog box in the current interface

  • This dialog box is the top above all the interface elements, you can shield the ability to interact with other controls

  • Therefore, AlertDialog general tips for some very important content or warnings

  • AlertDialog not new direct out

  • If we want to create AlertDialog, we need to use a static inner class in this class: public static class Builder

  • Then call AlertDialog in relevant methods to customize for AlertDialog

  • The last call to show () method to display the dialog our AlertDialog

• built-in method

  • setTitle: Settings dialog box titled

  • .setIcon: As dialog box settings icon

  • setMessage: Settings dialog box content

  • setPositiveButton: OK button's click event dialog settings

  • setNegativeButton (): Set the Cancel button click event for the dialog

  • setNeutralButton (): Set the neutral button click event for the dialog

  • setItems (): Create a simple list box

  • setSingleChoiceItems (): Create a single-selection list box

  • setMultiChoiceItems (): create a multiple-selection list box

• practical exercise of ordering - to create a multiple-selection list box

  Figure shows the effect of:

    

   Implementation code:

 1 import androidx.appcompat.app.AlertDialog;
 2 import androidx.appcompat.app.AppCompatActivity;
 3 
 4 import android.content.Context;
 5 import android.content.DialogInterface;
 6 import android.os.Bundle;
 7 import android.view.View;
 8 import android.widget.Button;
 9 import android.widget.Toast;
10 
11 public class MainActivity extends AppCompatActivity implements View.OnClickListener{
12 
13     private Button Btn2;
14     private AlertDialog alert = null;
15     private AlertDialog.Builder builder = null;
16     private boolean[] checkItems;
17     private String[] menu;
18     final private Context mContext = MainActivity.this;
19 
20     @Override
21     protected void onCreate(Bundle savedInstanceState) {
22         super.onCreate(savedInstanceState);
23         setContentView(R.layout.activity_main);
24          btn2 = the findViewById (R.id.btn_2);
 25          Btn2.setOnClickListener ( the this );
 26 is      }
 27  
28      public  void the onClick (View V) {
 29          Switch (v.getId ())
 30          {
 31 is              Case R.id.btn_2 :
 32                  Final String [] = MENU new new String [] { "pork stew noodles", "chicken mushroom stew", "Gongbaojiding", "Coke wings" };
 33 is                  checkItems = new new  Boolean [] { to false , to false , to false , to false };
34                 alert = null;
35                 builder = new AlertDialog.Builder(mContext);
36                 alert = builder.setTitle("菜单")
37                         .setMultiChoiceItems(menu, checkItems, new DialogInterface.OnMultiChoiceClickListener()
38                         {
39                             @Override
40                             public void onClick(DialogInterface dialog, int which, boolean isChecked)
41                             {
42                                 checkItems[which] = isChecked;
43                             }
44                         })
45                         .setPositiveButton("提交", new DialogInterface.OnClickListener()
46                         {
47                             @Override
48                             public void onClick(DialogInterface dialog, int which)
49                             {
50                                 int cnt = 0;
51                                 String result = "客官,您点了“";
52                                 boolean flag=false;
53                                 for(int i=0;i < checkItems.length;++i)
54                                 {
55                                     if(checkItems[i])
56                                     {
57                                         cnt++;
58                                         if(flag== true)
59                                             result += ",";
60                                         result += menu[i];
61                                         flag=true;
62                                     }
 63 is                                  }
 64                                  Result + = "", were "+ Integer.toString (cnt) +" dish! " ;
 65                                  Toast.makeText (mContext, Result, Toast.LENGTH_LONG) the .Show ();
 66                              }
 67                          })
 68                          .create (); // generate AlertDialog object via a Create () 
69                  Alert.show ();
 70                  BREAK ;
 71 is              default :
 72                  BREAK ;
 73 is          }
 74      }
75 }
MainActivity.java

  Realization bug occurred during:

    

  Error reason: setMultiChoiceItems () The second parameter should be a boolean [] type, and my checkItems is Boolean [] type;

  Solution: checkItems into boolean [] type on the line;

  References: Android create a multiple-choice menu, write setMultiChoiceItems () problems, suggesting solutions which do not exist

  Note: If you are calling by AlertDialog .show () method, then remember to generate a AlertDialog object last added .create builder.setXXXX of ();

 

Guess you like

Origin www.cnblogs.com/hyacinthLJP/p/12403590.html