Android的AlertDialog详解

时间:2023-06-29 05:40:26 

本文对Android的AlertDialog进行详细了解AlertDialog的构造方法全部是Protected的,所以不能直接通过new一个AlertDialog来创建出一个AlertDialog。
要创建一个AlertDialog,就要用到AlertDialog.Builder中的create()方法。
使用AlertDialog.Builder创建对话框需要了解以下几个方法:
setTitle :为对话框设置标题
setIcon :为对话框设置图标
setMessage:为对话框设置内容
setView : 给对话框设置自定义样式
setItems :设置对话框要显示的一个list,一般用于显示几个命令时
setMultiChoiceItems :用来设置对话框显示一系列的复选框
setNeutralButton    :普通按钮
setPositiveButton   :给对话框添加"Yes"按钮
setNegativeButton :对话框添加"No"按钮
create : 创建对话框
show :显示对话框
一、简单的AlertDialog
下面,创建一个简单的ALertDialog并显示它:

[java]  package com.tianjf; 
 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.Dialog; 
import android.os.Bundle; 
 
public class Dialog_AlertDialogDemoActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
 
        Dialog alertDialog = new AlertDialog.Builder(this). 
                setTitle("对话框的标题"). 
                setMessage("对话框的内容"). 
                setIcon(R.drawable.ic_launcher). 
                create(); 
        alertDialog.show(); 
    } 

package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
public class Dialog_AlertDialogDemoActivity extends Activity {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  Dialog alertDialog = new AlertDialog.Builder(this).
    setTitle("对话框的标题").
    setMessage("对话框的内容").
    setIcon(R.drawable.ic_launcher).
    create();
  alertDialog.show();
 }
}运行结果如下:
 Android的AlertDialog详解
 

二、带按钮的AlertDialog
上面的例子很简单,下面我们在这个AlertDialog上面加几个Button,实现删除操作的提示对话框

[java] package com.tianjf; 
 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.Dialog; 
import android.content.DialogInterface; 
import android.os.Bundle; 
 
public class Dialog_AlertDialogDemoActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
 
        Dialog alertDialog = new AlertDialog.Builder(this). 
                setTitle("确定删除?"). 
                setMessage("您确定删除该条信息吗?"). 
                setIcon(R.drawable.ic_launcher). 
                setPositiveButton("确定", new DialogInterface.OnClickListener() { 
                     
                    @Override 
                    public void onClick(DialogInterface dialog, int which) { 
                        // TODO Auto-generated method stub  
                    } 
                }). 
                setNegativeButton("取消", new DialogInterface.OnClickListener() { 
                     
                    @Override 
                    public void onClick(DialogInterface dialog, int which) { 
                        // TODO Auto-generated method stub  
                    } 
                }). 
                setNeutralButton("查看详情", new DialogInterface.OnClickListener() { 
                     
                    @Override 
                    public void onClick(DialogInterface dialog, int which) { 
                        // TODO Auto-generated method stub  
                    } 
                }). 
                create(); 
        alertDialog.show(); 
    } 

package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
public class Dialog_AlertDialogDemoActivity extends Activity {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  Dialog alertDialog = new AlertDialog.Builder(this).
    setTitle("确定删除?").
    setMessage("您确定删除该条信息吗?").
    setIcon(R.drawable.ic_launcher).
    setPositiveButton("确定", new DialogInterface.OnClickListener() {
     
     @Override
     public void onClick(DialogInterface dialog, int which) {
      // TODO Auto-generated method stub
     }
    }).
    setNegativeButton("取消", new DialogInterface.OnClickListener() {
     
     @Override
     public void onClick(DialogInterface dialog, int which) {
      // TODO Auto-generated method stub
     }
    }).
    setNeutralButton("查看详情", new DialogInterface.OnClickListener() {
     
     @Override
     public void onClick(DialogInterface dialog, int which) {
      // TODO Auto-generated method stub
     }
    }).
    create();
  alertDialog.show();
 }
}在这个例子中,我们定义了三个按钮,分别是"Yes"按钮,"No"按钮以及一个普通按钮,每个按钮都有onClick事件,TODO的地方可以放点了按钮之后想要做的一些处理
看一下运行结果:
 Android的AlertDialog详解

可以看到三个按钮添加到了AlertDialog上,三个没有添加事件处理的按钮,点了只是关闭对话框,没有任何其他操作。
 
 
 
三、类似ListView的AlertDialog
用setItems(CharSequence[] items, final OnClickListener listener)方法来实现类似ListView的AlertDialog
第一个参数是要显示的数据的数组,第二个参数是点击某个item的触发事件

[java] package com.tianjf; 
 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.Dialog; 
import android.content.DialogInterface; 
import android.os.Bundle; 
import android.widget.Toast; 
 
public class Dialog_AlertDialogDemoActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
 
        final String[] arrayFruit = new String[] { "苹果", "橘子", "草莓", "香蕉" }; 
 
        Dialog alertDialog = new AlertDialog.Builder(this). 
                setTitle("你喜欢吃哪种水果?"). 
                setIcon(R.drawable.ic_launcher) 
                .setItems(arrayFruit, new DialogInterface.OnClickListener() { 
  
                    @Override 
                    public void onClick(DialogInterface dialog, int which) { 
                        Toast.makeText(Dialog_AlertDialogDemoActivity.this, arrayFruit[which], Toast.LENGTH_SHORT).show(); 
                    } 
                }). 
                setNegativeButton("取消", new DialogInterface.OnClickListener() { 
 
                    @Override 
                    public void onClick(DialogInterface dialog, int which) { 
                        // TODO Auto-generated method stub  
                    } 
                }). 
                create(); 
        alertDialog.show(); 
    } 

package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;
public class Dialog_AlertDialogDemoActivity extends Activity {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  final String[] arrayFruit = new String[] { "苹果", "橘子", "草莓", "香蕉" };
  Dialog alertDialog = new AlertDialog.Builder(this).
    setTitle("你喜欢吃哪种水果?").
    setIcon(R.drawable.ic_launcher)
    .setItems(arrayFruit, new DialogInterface.OnClickListener() {
 
     @Override
     public void onClick(DialogInterface dialog, int which) {
      Toast.makeText(Dialog_AlertDialogDemoActivity.this, arrayFruit[which], Toast.LENGTH_SHORT).show();
     }
    }).
    setNegativeButton("取消", new DialogInterface.OnClickListener() {
     @Override
     public void onClick(DialogInterface dialog, int which) {
      // TODO Auto-generated method stub
     }
    }).
    create();
  alertDialog.show();
 }
}运行结果如下:
 Android的AlertDialog详解
 
 
 
四、类似RadioButton的AlertDialog
用setSingleChoiceItems(CharSequence[] items, int checkedItem, final OnClickListener listener)方法来实现类似RadioButton的AlertDialog
第一个参数是要显示的数据的数组,第二个参数是初始值(初始被选中的item),第三个参数是点击某个item的触发事件
在这个例子里面我们设了一个selectedFruitIndex用来记住选中的item的index

[java] package com.tianjf; 
 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.Dialog; 
import android.content.DialogInterface; 
import android.os.Bundle; 
import android.widget.Toast; 
 
public class Dialog_AlertDialogDemoActivity extends Activity { 
     
    private int selectedFruitIndex = 0; 
     
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
 
        final String[] arrayFruit = new String[] { "苹果", "橘子", "草莓", "香蕉" }; 
 
        Dialog alertDialog = new AlertDialog.Builder(this). 
                setTitle("你喜欢吃哪种水果?"). 
                setIcon(R.drawable.ic_launcher) 
                .setSingleChoiceItems(arrayFruit, 0, new DialogInterface.OnClickListener() { 
  
                    @Override 
                    public void onClick(DialogInterface dialog, int which) { 
                        selectedFruitIndex = which; 
                    } 
                }). 
                setPositiveButton("确认", new DialogInterface.OnClickListener() { 
 
                    @Override 
                    public void onClick(DialogInterface dialog, int which) { 
                        Toast.makeText(Dialog_AlertDialogDemoActivity.this, arrayFruit[selectedFruitIndex], Toast.LENGTH_SHORT).show(); 
                    } 
                }). 
                setNegativeButton("取消", new DialogInterface.OnClickListener() { 
 
                    @Override 
                    public void onClick(DialogInterface dialog, int which) { 
                        // TODO Auto-generated method stub  
                    } 
                }). 
                create(); 
        alertDialog.show(); 
    } 

package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;
public class Dialog_AlertDialogDemoActivity extends Activity {
 
 private int selectedFruitIndex = 0;
 
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  final String[] arrayFruit = new String[] { "苹果", "橘子", "草莓", "香蕉" };
  Dialog alertDialog = new AlertDialog.Builder(this).
    setTitle("你喜欢吃哪种水果?").
    setIcon(R.drawable.ic_launcher)
    .setSingleChoiceItems(arrayFruit, 0, new DialogInterface.OnClickListener() {
 
     @Override
     public void onClick(DialogInterface dialog, int which) {
      selectedFruitIndex = which;
     }
    }).
    setPositiveButton("确认", new DialogInterface.OnClickListener() {
     @Override
     public void onClick(DialogInterface dialog, int which) {
      Toast.makeText(Dialog_AlertDialogDemoActivity.this, arrayFruit[selectedFruitIndex], Toast.LENGTH_SHORT).show();
     }
    }).
    setNegativeButton("取消", new DialogInterface.OnClickListener() {
     @Override
     public void onClick(DialogInterface dialog, int which) {
      // TODO Auto-generated method stub
     }
    }).
    create();
  alertDialog.show();
 }
}
运行结果如下:
 Android的AlertDialog详解
 

五、类似CheckBox的AlertDialog
用setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems, final OnMultiChoiceClickListener listener)方法来实现类似CheckBox的AlertDialog
第一个参数是要显示的数据的数组,第二个参数是选中状态的数组,第三个参数是点击某个item的触发事件

[java] package com.tianjf; 
 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.Dialog; 
import android.content.DialogInterface; 
import android.os.Bundle; 
import android.widget.Toast; 
 
public class Dialog_AlertDialogDemoActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
 
        final String[] arrayFruit = new String[] { "苹果", "橘子", "草莓", "香蕉" }; 
        final boolean[] arrayFruitSelected = new boolean[] {true, true, false, false}; 
 
        Dialog alertDialog = new AlertDialog.Builder(this). 
                setTitle("你喜欢吃哪种水果?"). 
                setIcon(R.drawable.ic_launcher) 
                .setMultiChoiceItems(arrayFruit, arrayFruitSelected, new DialogInterface.OnMultiChoiceClickListener() { 
                     
                    @Override 
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) { 
                        arrayFruitSelected[which] = isChecked; 
                    } 
                }). 
                setPositiveButton("确认", new DialogInterface.OnClickListener() { 
 
                    @Override 
                    public void onClick(DialogInterface dialog, int which) { 
                        StringBuilder stringBuilder = new StringBuilder(); 
                        for (int i = 0; i < arrayFruitSelected.length; i++) { 
                            if (arrayFruitSelected[i] == true) 
                            { 
                                stringBuilder.append(arrayFruit[i] + "、"); 
                            } 
                        } 
                        Toast.makeText(Dialog_AlertDialogDemoActivity.this, stringBuilder.toString(), Toast.LENGTH_SHORT).show(); 
                    } 
                }). 
                setNegativeButton("取消", new DialogInterface.OnClickListener() { 
 
                    @Override 
                    public void onClick(DialogInterface dialog, int which) { 
                        // TODO Auto-generated method stub  
                    } 
                }). 
                create(); 
        alertDialog.show(); 
    } 

package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;
public class Dialog_AlertDialogDemoActivity extends Activity {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  final String[] arrayFruit = new String[] { "苹果", "橘子", "草莓", "香蕉" };
  final boolean[] arrayFruitSelected = new boolean[] {true, true, false, false};
  Dialog alertDialog = new AlertDialog.Builder(this).
    setTitle("你喜欢吃哪种水果?").
    setIcon(R.drawable.ic_launcher)
    .setMultiChoiceItems(arrayFruit, arrayFruitSelected, new DialogInterface.OnMultiChoiceClickListener() {
     
     @Override
     public void onClick(DialogInterface dialog, int which, boolean isChecked) {
      arrayFruitSelected[which] = isChecked;
     }
    }).
    setPositiveButton("确认", new DialogInterface.OnClickListener() {
     @Override
     public void onClick(DialogInterface dialog, int which) {
      StringBuilder stringBuilder = new StringBuilder();
      for (int i = 0; i < arrayFruitSelected.length; i++) {
       if (arrayFruitSelected[i] == true)
       {
        stringBuilder.append(arrayFruit[i] + "、");
       }
      }
      Toast.makeText(Dialog_AlertDialogDemoActivity.this, stringBuilder.toString(), Toast.LENGTH_SHORT).show();
     }
    }).
    setNegativeButton("取消", new DialogInterface.OnClickListener() {
     @Override
     public void onClick(DialogInterface dialog, int which) {
      // TODO Auto-generated method stub
     }
    }).
    create();
  alertDialog.show();
 }
}运行结果如下:
 Android的AlertDialog详解
 
 
六、自定义View的AlertDialog
有时候我们不能满足系统自带的AlertDialog风格,就比如说我们要实现一个Login画面,有用户名和密码,这时我们就要用到自定义View的AlertDialog
先创建Login画面的布局文件
[html]

标签:Android,AlertDialog
0
投稿

猜你喜欢

  • 谷歌新自主内核OS曝光 抛弃Android底层

    2022-10-11 05:38:52
  • Win11 Dev无法切换Beta通道怎么办?

    2023-07-02 14:33:04
  • 希望能对大家有所帮助! 1、数据去重 图中第1列中的数据是重复的 选中要进行去重的数据

    2022-10-25 08:07:40
  • Win11如何开启超级管理员 Win11开启超级管理员的教程

    2023-08-19 11:06:17
  • excel表格如何把相同的内容找出

    2022-07-10 07:25:31
  • word如何复制表格并保持原格式

    2023-06-10 02:43:35
  • excel表格怎么指定数据剔除?

    2023-12-21 02:55:30
  • 如何在 Mac 上安装新字体?

    2022-01-16 15:22:23
  • 如何更改excel工作簿中默认的工作表张数

    2022-09-10 10:24:00
  • excel如何删除重复项

    2023-11-11 18:44:22
  • Excel表格中快速实现对销售员的业绩汇总、占比和排名的方法

    2023-07-25 01:14:36
  • 掌握word段落的文字间距的控制方法 让文字不错位挤在在一起

    2022-10-22 11:45:51
  • 柴扉APP如何收藏文章 柴扉APP收藏文章方法

    2022-02-28 04:41:09
  • MAC系统中Command键功能有哪些

    2023-05-12 09:46:15
  • 深度解决开始菜单和Cortana无法工作的问题

    2023-12-29 08:48:36
  • excel表中自动汇总数据

    2022-02-24 07:10:54
  • 小丸工具箱怎么压缩视频大小?小丸工具箱压缩视频参数设定教程

    2023-09-11 16:01:50
  • excel表格兼容模式怎么设置

    2022-11-08 04:44:20
  • 如何强制清理Mac OS X内存让电脑运行速度更快

    2023-03-05 23:57:43
  • Windows 10已经更名RTM 正式版即将到来

    2023-01-04 01:19:45
  • asp之家 电脑教程 m.aspxhome.com