Android中显示提醒对话框问题

问题描述:

我想实现一个弹出窗口“确定要删除该项目吗?”
或者点击按钮删除。
如果我点击确认按钮,该项就删除了。
我用代码写了点击按钮的监听器,但不知道用什么来调用对话框或者弹出菜单,来实现这个功能…

你可以使用alert builder:

new AlertDialog.Builder(this)
    .setTitle("Delete entry")
    .setMessage("Are you sure you want to delete this entry?")
    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) { 
            // continue with delete
        }
     })
    .setNegativeButton("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) { 
            // do nothing
        }
     })
     .show();

@Override
protected Dialog onCreateDialog(int id)
{
    switch(id)
    {
    case 0:
    {               
        return new AlertDialog.Builder(this)
        .setMessage("set Message here")
        .setPositiveButton("set button text here", new DialogInterface.OnClickListener() 
        {                   
            @Override
            public void onClick(DialogInterface arg0, int arg1) 
            {
                try
                {

                }
                catch(Exception e)
                {
                    Toast.makeText(getBaseContext(),  "", Toast.LENGTH_LONG).show();
                }
            }
        }).create();                
    }
  }
    return null;
}