本文介绍了无法驳回AlertDialog时pressing菜单键第二次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里呈现出 AlertDialog 通过pressing菜单键和显示弹出对话框,但当我试图 dissmiss 对话框由$ p $再次pssing菜单键第二次它似乎菜单键不触发。

这是我的code此

  @燮pressLint({NewApi,NewApi,NewApi,NewApi})
    @覆盖
 公共布尔的onkeydown(INT键code,KeyEvent的事件){
  AlertDialog.Builder alertDialogBu​​ilder =新AlertDialog.Builder(背景);    如果(键code == KeyEvent.KEY code_MENU&放大器;&安培; event.getAction()== 0){   如果(!打开){
   showDialogItem();
   开放= TRUE;
   }   否则,如果(打开){
   alertDialog.dismiss(
  开放= FALSE;
   }返回true;            }    返回super.onKeyDown(键code,事件);    } 公共无效showDialogItem(){
                     AlertDialog.Builder alertDialogBu​​ilder =新AlertDialog.Builder(背景);
  alertDialogBu​​ilder .setItems(R.array.select_dialog_items,新DialogInterface.OnClickListener()
     {
  公共无效的onClick(DialogInterface对话,诠释它){
 。的String []项目= getResources()getStringArray(R.array.select_dialog_items);
 新AlertDialog.Builder(MainActivity.this)
 .setMessage(您选择:这+ +,+项目[它])
                                            。显示();
                                }
                            });
   alertDialog = alertDialogBu​​ilder.create();   WindowManager.LayoutParams LP =新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)。    }


解决方案

 得到这个初始化公共静态INT X = 0;公共布尔的onkeydown(INT键code,KeyEvent的事件){如果(键code == KeyEvent.KEY code_MENU&放大器;&安培; event.getAction()== 0)
{AlertDialog.Builder建设者=新AlertDialog.Builder(本)
.setOnKeyListener(新DialogInterface.OnKeyListener()
{
 @覆盖
 公共布尔安其(DialogInterface对话,诠释关键code,KeyEvent的事件)
{         如果(键code == KeyEvent.KEY code_MENU&放大器;&安培; event.getAction()== 0)
{                      如果(X == 1)
                      {
                        alert.dismiss();
                        X = 0;
                      }
             返回true;
             }
             返回false;
             }
             });
如果(X == 0)
{
builder.setMessage(登录信息修改成功......);
建设者.setCancelable(真);
builder.setPositiveButton(OK,新DialogInterface.OnClickListener(){
公共无效的onClick(DialogInterface对话,诠释的id){
      System.exit(0);
 }
     });
    警报= builder.create();
    alert.show();
    X = 1;
          }
          返回true;         }

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

    }
解决方案
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;

         }

这篇关于无法驳回AlertDialog时pressing菜单键第二次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 15:22