0

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

4

2 回答 2

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

    }
于 2012-05-24T05:27:38.427 回答
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();
}

这将创建一个具有复选框和名称的 AlertDialog .....

于 2012-05-24T06:08:05.320 回答