如何在 Android 中显示警报时检测搜索按钮

发布于 2024-11-27 04:57:21 字数 199 浏览 1 评论 0原文

大家好,stackoverflow 的朋友们,

我最近遇到了一个问题,当屏幕上显示警报时,如何禁用 Android 中的全局搜索按钮。我不想通过使用搜索按钮来消失警报框。我需要用户必须单击警报框按钮并以这种方式消失。所以我想在显示警报框时禁用搜索按钮。但我可以使用 setCancable(false) 禁用后退按钮。我该如何解决这个问题?

提前致谢。

Hi stackoverflow friends

I recently faced an issue that how can i disable global search button in android while an alert is shown in the screen.I don't want to disappear the alert box by using search button. I need to user must click the alertbox button and disappears in that way.So I want to disable the search button while alert box is shown. But I can disable the back button using setCancable(false).How can I solve this ?

THanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

屌丝范 2024-12-04 04:57:21

因此,您的意图是提供不可取消的警报。
建议设置 OnDismissListener 并再次显示警报。从视觉角度来看这不是很好(警报关闭然后再次打开)。
下面是一些如何实现此类不可取消警报的明显示例(代码位于 Acctivity 类内):

/** reference to our alert */
private AlertDialog alert = null;
/** to indicate if alert dismissed by key */
private boolean alertKeyPressed = false;

@Override
protected void onResume() {

    // Say, we need to show alert when activity resumed

    if(true/*provide condition to show alert*/) {

        showAlert();
    }
}

/**
 * Show non dismissable alert
 */
private void showAlert() {
    if(null == this.alert) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.setCancelable(false);
        builder.setTitle(R.string.str_alert_title);
        builder.setMessage(R.string.str_alert_text);
        builder.setIcon(android.R.drawable.ic_dialog_alert);

        builder.setNeutralButton("OK", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

                YourActivity.this.alertKeyPressed = true;

                dialog.dismiss();
            }
        });

        this.alert = builder.create();

        this.alert.setOwnerActivity(this);
        this.alert.show();

        this.alert.setOnDismissListener(new DialogInterface.OnDismissListener() {

            @Override
            public void onDismiss(DialogInterface dialog) {


                // dialog is not allowed to be dismissed, so show it again
                YourActivity.this.alert = null;

                if(!YourActivity.this.alertKeyPressed) {
                    showAlert();
                }
            }
        });
    }
}

但是,我认为这不是为用户留下此类警报的正确方法,有时在评估限制等情况下可能需要它ETC。

So, Your intention is to provide non-cancelable alert.
Suggesting to set OnDismissListener and just show alert again. It's not very good from visual perspective (alert get closed and opened again).
Below is some obvious example how to achieve such non-cancelable alert (code is inside Acctivity class):

/** reference to our alert */
private AlertDialog alert = null;
/** to indicate if alert dismissed by key */
private boolean alertKeyPressed = false;

@Override
protected void onResume() {

    // Say, we need to show alert when activity resumed

    if(true/*provide condition to show alert*/) {

        showAlert();
    }
}

/**
 * Show non dismissable alert
 */
private void showAlert() {
    if(null == this.alert) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.setCancelable(false);
        builder.setTitle(R.string.str_alert_title);
        builder.setMessage(R.string.str_alert_text);
        builder.setIcon(android.R.drawable.ic_dialog_alert);

        builder.setNeutralButton("OK", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

                YourActivity.this.alertKeyPressed = true;

                dialog.dismiss();
            }
        });

        this.alert = builder.create();

        this.alert.setOwnerActivity(this);
        this.alert.show();

        this.alert.setOnDismissListener(new DialogInterface.OnDismissListener() {

            @Override
            public void onDismiss(DialogInterface dialog) {


                // dialog is not allowed to be dismissed, so show it again
                YourActivity.this.alert = null;

                if(!YourActivity.this.alertKeyPressed) {
                    showAlert();
                }
            }
        });
    }
}

However, I don't think it's the right way to left such alert for the user, sometimes it might be needed for cases like evaluation restriction etc.

北风几吹夏 2024-12-04 04:57:21

覆盖 Activity 中的 onSearchRequested 并使其在显示对话框时返回 false。这应该会阻止请求,根据 docs

您可以覆盖此函数以强制全局搜索,例如在
响应专用搜索键,或完全阻止搜索(通过
简单地返回 false)。

如果搜索已启动,则返回 true;如果活动阻止搜索,则返回 false。这
默认实现始终返回 true。

Override onSearchRequested in your Activity and have it return false while the dialog is being shown. This should block the request, as per the docs:

You can override this function to force global search, e.g. in
response to a dedicated search key, or to block search entirely (by
simply returning false).

Returns true if search launched, and false if activity blocks it. The
default implementation always returns true.

手心的海 2024-12-04 04:57:21
.setOnKeyListener(new DialogInterface.OnKeyListener()
{   
    @Override
    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
    {
        //There you catch the key, do whatever you want to do.
        //Return true if you handled the key event, so nothing will trigger.
        //Return false if you want your activity to handle.
        return true;
    }
})

只需将上面的代码添加到警报对话框生成器即可。希望这个片段会有所帮助。祝你好运。

.setOnKeyListener(new DialogInterface.OnKeyListener()
{   
    @Override
    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
    {
        //There you catch the key, do whatever you want to do.
        //Return true if you handled the key event, so nothing will trigger.
        //Return false if you want your activity to handle.
        return true;
    }
})

Just add the code above to alert dialog builder. Hope this snippet would help. Good luck.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文