简体   繁体   English

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

[英]how to detect a search button while an alert is shown in an android

Hi stackoverflow friends 嗨stackoverflow朋友

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. 我最近遇到了一个问题,当屏幕上显示警报时如何禁用android中的全局搜索按钮。我不想通过使用搜索按钮来消失警报框。 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 ? 但是我可以使用setCancable(false)禁用后退按钮。如何解决这个问题?

THanks in advance. 提前致谢。

So, Your intention is to provide non-cancelable alert. 因此,您的意图是提供不可取消的警报。 Suggesting to set OnDismissListener and just show alert again. 建议设置OnDismissListener并再次显示警报。 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): 以下是一些明显的示例,说明如何实现这种不可取消的警报(代码在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();
                }
            }
        });
    }
}

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. 但是,我认为这不是向用户发出此类警报的正确方法,有时对于诸如评估限制之类的情况可能是必需的。

Override onSearchRequested in your Activity and have it return false while the dialog is being shown. 覆盖您的Activity中的onSearchRequested ,并在显示对话框时使其返回false This should block the request, as per the docs : 根据docs ,这应该阻止请求:

You can override this function to force global search, eg in response to a dedicated search key, or to block search entirely (by simply returning false). 您可以覆盖此功能以强制进行全局搜索(例如,响应于专用搜索键)或完全阻止搜索(只需返回false)。

Returns true if search launched, and false if activity blocks it. 如果启动搜索,则返回true;如果活动阻止,则返回false。 The default implementation always returns true. 默认实现始终返回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. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM