开发者

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

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开发者_StackOverflow advance.


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.


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.


.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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜