The use of 7 kinds of dialog boxes in android

In Android development, we often need to pop up some dialog boxes on the Android interface, such as asking the user or letting the user choose. We call these functions the Android Dialog dialog box. In the process of using Android, I have summarized it. There are only 7 types of Android Dialog dialog boxes. Below I will introduce the usage methods of these 7 kinds of Android Dialog dialog boxes. Hope to be helpful to everyone.

1. Picture link http://s7.51cto.com/wyfs01/M01/2F/F3/wKioJlJI4NLivGhoAABHQcKFnzE744.jpg

The effect is that when you press the back button, a prompt will pop up to ensure correct operation, using a common dialog box style.

The code for creating a dialog is as follows:

    protected void dialog() {  
    
      AlertDialog.Builder builder = new Builder(Main.this);  
    
      builder.setMessage("Are you sure to exit?");  
    
       builder.setTitle("Prompt");  
    
       builder. setPositiveButton("Confirm", new  
    
       OnClickListener() { @Override 
    
       public void onClick(DialogInterface dialog, int which) {  
    
         dialog.dismiss();  
    
        Main.this.finish();  
    
       }  
    
      });  
    
       builder.setNegativeButton("取消", new OnClickListener() {  
    
       @Override 
    
        public void onClick(DialogInterface dialog, int which) {  
    
         dialog.dismiss();  
    
       }  
    
      });  
    
       builder.create().show();  
    
      } 

在onKeyDown(int keyCode, KeyEvent event)方法中调用此方法

    public boolean onKeyDown(int keyCode, KeyEvent event) {  
    
      if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {  
    
       dialog();  
    
      }  
    
      return false;  
    
      } 

2. Diagram link http://s8.51cto.com/wyfs01/M00/2F/F5/wKioOVJI4NLjdSkDAABMEtk3Xiw531.jpg

Changed the diagram of the dialog and added three buttons. The method code for

creating the dialog is as follows:

    Dialog dialog = new AlertDialog. Builder(this).setIcon(  
    
          android.R.drawable.btn_star).setTitle("Preference survey").setMessage(  
    
         "Do you like Jet Li's movies?").setPositiveButton("I like it very much",  
    
         new OnClickListener() {  
    
          @ Override 
    
          public void onClick(DialogInterface dialog, int which) {  
    
           // TODO Auto-generated method stub  
    
            Toast.makeText(Main.this, "I like his movies.",  
    
              Toast.LENGTH_LONG).show();  
    
          }  
    
         }) .setNegativeButton("Don't like",   new OnClickListener() {  
    
        @Override 
    
        public void onClick(DialogInterface dialog, int which) {  
    
         // TODO Auto-generated method stub  
    
         Toast.makeText(Main.this, "I don't like his movies.", Toast.LENGTH_LONG)  
    
            .show();  
    
         }  
    
       } ).setNeutralButton("General", new  
    
        OnClickListener() { @Override 
    
        public void onClick(DialogInterface dialog, int which) {  
    
         // TODO Auto-generated method stub  
    
         Toast.makeText(Main.this, "I don't like it or not. ", Toast.LENGTH_LONG)  
    
            .show();  
    
        }  
    
       }).create();  
    
       dialog.show(); 

3. Figure link http://s8.51cto.com/wyfs01/M00/2F/F5/wKioOVJI4NKQye7IAABLw_p5eIw935.jpg
The information content is a simple View type The code

to create the dialog method is as follows:

    new AlertDialog.Builder(this).setTitle( "Please input").setIcon(  
    
          android.R.drawable.ic_dialog_info).setView(  
    
          new EditText(this)).setPositiveButton("OK", null)  
    
         .setNegativeButton("Cancel", null).show(); 

4. http://s3.51cto.com/wyfs01/M02/2F/F3/wKioJlJI4NLCGISjAABJlGxCi4U119.jpg

The information content is a set of radio buttons The code

to create the dialog method is as follows:

    new AlertDialog.Builder(this).setTitle("Check box ").setMultiChoiceItems(  
    
          new String[] { "Item1", "Item2" }, null, null)  
    
         .setPositiveButton("OK", null)  
    
         .setNegativeButton("Cancel", null).show(); 

5.http://s3.51cto.com/wyfs01/M02/2F/F3/wKioJlJI4NLwla4yAABNh3CfsJE169.jpg The

information content is a set of multi-select boxes The code to

create the dialog method As follows:

    new AlertDialog.Builder(this).setTitle("radio box").setIcon(  
    
          android.R.drawable.ic_dialog_info).setSingleChoiceItems(  
    
          new String[] { "Item1", "Item2" }, 0,  
    
          new DialogInterface .OnClickListener() {  
    
           public void onClick(DialogInterface dialog, int which) {  
    
            dialog.dismiss();  
    
           }  
    
          }).setNegativeButton("Cancel", null).show(); 

6. http://s5.51cto.com /wyfs01/M01/2F/F5/wKioOVJI4NPQT7jjAABJv4-sZ-g328. jpg

information content is a set of simple list items

The method code for creating the dialog is as follows:

    new AlertDialog.Builder(this).setTitle("List Box").setItems(  
    
          new String[] { "Item1", "Item2" }, null).setNegativeButton(  
    
         "OK", null) .show(); 

7.http://s7.51cto.com/wyfs01/M01/2F/F5/wKioOVJI4NPiFq0kAABWUgb9JOc883.jpg The

information content is a custom layout The

dialog layout file code is as follows:

    <?xml version="1.0" encoding="utf-8"?>  
    
        
    
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    
      android:layout_height="wrap_content" android:layout_width="wrap_content" 
    
      android:background= "#ffffffff" android:orientation="horizontal" 
    
     android:id="@+id/dialog"   >  
    
      <TextView android:layout_height="wrap_content" 
    
        android:layout_width="wrap_content" 
    
       android:id="@+id/tvname" android:text="姓名:" />  
    
      <EditText android:layout_height="wrap_content" 
    
      android:layout_width="wrap_content" android:id="@+id/etname" android:minWidth="100dip"/>  
    
    </LinearLayout> 

创建dialog方法的代码如下:

    LayoutInflater inflater = getLayoutInflater();  
    
       View layout = inflater.inflate(R.layout.dialog,  
    
         (ViewGroup) findViewById(R.id.dialog));  
    
       new AlertDialog.Builder(this).setTitle("自定义布局").setView(layout)  
    
          .setPositiveButton("确定", null)  
    
         .setNegativeButton("取消",null).show(); 

Well, the usage methods of the above 7 kinds of Android dialog dialog boxes are introduced here, and they are basically all. If you encounter dialog in the android development process, you can take it out and take a look.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326606004&siteId=291194637