Warm tip: This article is reproduced from serverfault.com, please click

android-如何在对话框上显示适当的图标

(android - How to show appropriate icon on dialog box)

发布于 2011-07-14 12:41:42

我有一个允许用户删除视频文件的应用程序。当我按下删除按钮时,我正在使用

DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        switch (which) {
        case DialogInterface.BUTTON_POSITIVE:
            // mycode........
            break;
        case DialogInterface.BUTTON_NEGATIVE:
            // mycode.....
            break;
        }
    }
};

但是此消息没有警告或删除图标,正如我们在android设备中看到的那样。谁能帮助我获取这些图标或使用任何其他可以显示这些图标的警报对话框?

Questioner
Farhan
Viewed
0
9,493 2014-07-15 19:14:04

我倾向于使用AlertDialog.Builder,就像它们在官方doc示例中显示的那样

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
   .setCancelable(false)
   .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
            MyActivity.this.finish();
       }
   })
   .setNegativeButton("No", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
       }
   })
   //Set your icon here
   .setTitle("Alert!")
   .setIcon(R.drawable.icon);
AlertDialog alert = builder.create();
 alert.show();//showing the dialog

至于实际的图标,请查看你的sdk文件夹/平台/ android版本#/ data / res / drawable-mdpi或其他内容