2012-05-24 144 views
0

我想用项目列表显示一个AlertDialog。该列表应该是二维的。在按下a按钮时,应显示对话框。那么我该怎么做呢?是否需要为警告对话框单独创建一个xml文件,还是应该将对话框包含在java代码本身中?如何在android中显示警报对话框?

+0

如果你知道如何定义自定义适配器的话很容易给你,你所提到的定义自定义对话框的AlertDialog。 –

+0

你可以创建对话框......这将包括你使用'setView()'方法创建的xmlfile – c2dm

+0

,你可以设置任何视图到你的警报对话框。 –

回答

3

要创建警报对话框,

public void Alert(String text, String title) 
    { 
     AlertDialog dialog=new AlertDialog.Builder(context).create(); 
     dialog.setTitle(title); 
     dialog.setMessage(text); 
     if(!title.equals("") && !text.equals("")) 
     { 
      dialog.setButton("OK", 
        new DialogInterface.OnClickListener() 
        { 
         public void onClick(DialogInterface dialog, int whichButton) 
         { 
          // 
         } 
        }); 
      dialog.setButton2("Cancel", 
        new DialogInterface.OnClickListener() 
        { 
         public void onClick(DialogInterface dialog, int whichButton) 
         { 
          // 
         } 
        }); 
     } 

     dialog.show(); 

    } 
+0

谢谢你的代码,但是我应该为DialogInterface包含哪些包...因为它在Dialog Interface上显示一些错误 – thedarkpassenger

+0

import android.content.DialogInterface; – Ponmalar

+0

感谢和它的工作......我需要做什么来在列表中的每一行中包含2个项目? – thedarkpassenger

0

你为什么不建立一个对话的主题活动,流行起来,而不是对话的?

如果你坚持创建一个对话框。这是一段你可以尝试的代码。

//Class Level Variables: 
CharSequence[] items = { "Google", "Apple", "Microsoft" }; 
boolean[] itemsChecked = new boolean [items.length]; 

//Call this when you want a dialog 
showdialog(0); 

//override onCreateDialog 
@Override 
protected Dialog onCreateDialog(int id) { 
    switch (id) { 
    case 0:   
     return new AlertDialog.Builder(this) 
     .setIcon(R.drawable.icon) 
     .setTitle("This is a dialog with some simple text...") 
     .setPositiveButton("OK", new 
      DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, 
      int whichButton) 
      { 
       Toast.makeText(getBaseContext(), 
        "OK clicked!", Toast.LENGTH_SHORT).show(); 
      } 
     }) 
     .setNegativeButton("Cancel", new 
      DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, 
       int whichButton) 
      { 
       Toast.makeText(getBaseContext(), 
        "Cancel clicked!", Toast.LENGTH_SHORT).show(); 
      } 
     })    
     .setMultiChoiceItems(items, itemsChecked, new 
      DialogInterface.OnMultiChoiceClickListener() {     
       @Override 
       public void onClick(DialogInterface dialog, int which, boolean isChecked) { 
        Toast.makeText(getBaseContext(), 
         items[which] + (isChecked ? " checked!": " unchecked!"), 
         Toast.LENGTH_SHORT).show(); 
       } 
      } 
     ) 
     .create(); 
} 

这将创建都有一个复选框和名称.....

+0

来源:http://www.wrox.com。 – drulabs