2011-07-14 22 views
7

我有一个允许用户删除视频文件的应用程序。当我按下删除按钮,我使用如何在对话框上显示适当的图标

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设备上看到的。任何人都可以帮助我获取这些图标或使用任何可显示这些图标的其他警告对话框吗?

+1

请张贴的代码,你在哪里创建AlertDialog。 –

+0

先生,我已经使用对话框(不提醒对话框),并希望使用警报diaglog的例子..你能给我一个.. – Farhan

回答

25

我倾向于像他们在官方文档例子

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版#/数据/ RES /提拉 - MDPI内显示使用AlertDialog.Builder什么

+0

我试图设置绘制,但它没有显示在对话框 – Farhan

+1

嗯,尝试设置一个标题也是。我认为这可能是你为什么没有看到一个。尝试我编辑的代码。 –

+2

凯文也对尝试设置标题。 –

0

只需更换.setIcon(R.drawable.icon);.setIcon(getResources().getDrawable(R.drawable.icon)); 但它已过时。

1

为了设置默认的对话框图标使用:

.setIcon(android.R.drawable.ic_dialog_alert) 

有可用的几个图标:

  • android.R.drawable.ic_dialog_dialer
  • android.R.drawable .ic_dialog_info
  • android.R.drawable.ic_dialog_email
  • android.R.drawable.ic_dialog_map
0

另一个(脏)的方式:

TypedValue typedValue = new TypedValue(); 
getTheme().resolveAttribute(android.R.attr.alertDialogIcon, typedValue, true); 

new AlertDialog.Builder(this) 
    .setIcon(typedValue.resourceId) 
    ... 
    .show();