Not able to Dismiss the AlertDialog when pressing

2019-06-11 21:53发布

Here I am showing a AlertDialog By pressing a menu key and its displaying the popup dialogs but when i am trying to dissmiss the dialog by pressing the menu key again 2nd time its seems the menu key doesn't triggered.

here is my code for this

     @SuppressLint({ "NewApi", "NewApi", "NewApi", "NewApi" })
    @Override
 public boolean onKeyDown(int keyCode, KeyEvent event) {
  AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);

    if (keyCode == KeyEvent.KEYCODE_MENU && event.getAction() == 0 ) {

   if(!open){
   showDialogItem();
   open=true;


   }

   else if(open){
   alertDialog.dismiss(
  open =false;
   }return true;

            }

    return super.onKeyDown(keyCode, event);

    } 

 public void showDialogItem(){
                     AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
  alertDialogBuilder .setItems(R.array.select_dialog_items, new DialogInterface.OnClickListener()
     {
  public void onClick(DialogInterface dialog, int which) {


 String[] items = getResources().getStringArray(R.array.select_dialog_items);
 new AlertDialog.Builder(MainActivity.this)
 .setMessage("You selected: " + which + " , " + items[which])
                                            .show();
                                }
                            });


   alertDialog = alertDialogBuilder.create();

   WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp = alertDialog.getWindow().getAttributes();
 lp.gravity = Gravity.TOP | Gravity.LEFT;
   lp.x=-300;
lp.gravity=Gravity.BOTTOM;
   alertDialog.getWindow().setAttributes(lp);

    alertDialog.show();
    alertDialog.getWindow().setLayout(130, 220);

    }

标签: android menu
3条回答
Summer. ? 凉城
2楼-- · 2019-06-11 22:28

initialize open=false;

     if(open==false){
            showDialogItem();
            open=true;             
             }

     else{
           alertDialog.dismiss();
           open =false;
         }return true;
查看更多
Root(大扎)
3楼-- · 2019-06-11 22:30
Got this initialize 

public static int x=0;

public boolean onKeyDown(int keyCode, KeyEvent event) {

if (keyCode == KeyEvent.KEYCODE_MENU && event.getAction() == 0 ) 
{

AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setOnKeyListener(new DialogInterface.OnKeyListener() 
{
 @Override
 public boolean onKey (DialogInterface dialog, int keyCode, KeyEvent event) 
{

         if (keyCode == KeyEvent.KEYCODE_MENU && event.getAction() == 0) 
{

                      if(x==1)
                      {
                        alert.dismiss();
                        x=0;
                      }
             return true;
             }
             return false;
             }
             });
if(x==0)
{
builder.setMessage("Login details Changed Successfully...");
builder .setCancelable(true);              
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
      System.exit(0);
 }
     });
    alert = builder.create();
    alert.show();
    x=1;
          }
          return true;

         }
查看更多
【Aperson】
4楼-- · 2019-06-11 22:39

This will not work as you expect, simply because once the dialog box is open, it will intercept the menu key and not the underlying activity. You can work around it is not to use an AlertDialog. Instead, you can create an activity that is presented as a dialog:

Then from your main activity, on menu button press, you start this activity. And in this activity, on menu button press, you finish it.

The other alternative would be to add a key listener to the dialog and dismiss the dialog when the menu button is pressed.

In either of the two solutions, the important point to note is that it's the dialog (or the dialog activity) and not the parent activity that will receive the second menu press.

查看更多
登录 后发表回答