【问题标题】:How to show Alert Dialog in android?如何在android中显示警报对话框?
【发布时间】:2012-05-24 05:13:23
【问题描述】:

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

【问题讨论】:

标签: android android-ui android-alertdialog


【解决方案1】:

要创建警报对话框,

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();

    }

【讨论】:

  • 感谢代码,但我应该为 DialogInterface 包含哪个包...因为它在 Dialog Interface 上显示了一些错误
  • 导入android.content.DialogInterface;
  • 谢谢,它的工作原理......我必须做些什么才能在列表中的每一行中包含 2 个项目?
  • 这使用了gridview..有没有其他方法可以在没有gridview帮助的情况下显示两个元素
【解决方案2】:

为什么不创建一个以对话框为主题的活动并弹出它而不是对话框?

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

//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();
}

这将创建一个 AlertDialog,其中包含一个复选框和名称.....

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-08
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    相关资源
    最近更新 更多