【问题标题】:How to keep search button from closing an AlertDialog?如何防止搜索按钮关闭 AlertDialog?
【发布时间】:2010-09-30 23:37:03
【问题描述】:

如果我使用构建器创建独立的警报对话框(未连接到活动/视图),如何防止搜索按钮导致警报对话框关闭?

谢谢。

【问题讨论】:

  • 我不知道,但我很想看到有人回答这个问题。使用 keyEvent 类不起作用,因为在打开对话框时触摸搜索键时不会执行 OnKeyUp/Down/Longpress。我想知道是否有其他方法可以被覆盖。
  • 即使我也有同样的问题......有什么想法吗?
  • 对 KeyEvent 和 onSearchRequested() 不执行任何操作。你试过吗?
  • 你试过builder.setCancelable(false);吗?

标签: android


【解决方案1】:

这真的很棘手,我想到了 LayoutInflater 但那东西也需要关闭。一个俗气的方法是有一个你只是可见或不可见的视图

【讨论】:

    【解决方案2】:

    我在显示 EULA 对话框时也遇到了同样的问题。 由 setOnKeyListener 解决。

    解决办法如下:

                    AlertDialog.Builder builder = new AlertDialog.Builder(mActivity)
                        .setTitle(title)
                        .setMessage(message)
                        .setPositiveButton(android.R.string.ok, new Dialog.OnClickListener() {
    
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                SharedPreferences.Editor editor = prefs.edit();
                                editor.putInt(Constants.EULA_VERSION, versionInfo.versionCode);
                                editor.commit();
                                dialogInterface.dismiss();
                            }
                        })
                        .setNegativeButton(android.R.string.cancel, new Dialog.OnClickListener() {
    
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // Close the activity once the EULA is declined.
                                mActivity.finish(); 
                            }
    
                        });
    
                //To avoid skipping EULA screen through search & menu button.
                builder.setOnKeyListener(new DialogInterface.OnKeyListener() {
                    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                        if (keyCode < KeyEvent.KEYCODE_DPAD_UP || keyCode > KeyEvent.KEYCODE_DPAD_CENTER) 
                        {
                            return true;
                        }
                        else
                            return false;
                    }
                });
                builder.create().show();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-30
      • 1970-01-01
      • 2018-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多