Android: Create a alertdialog dialog box, you must press the OK or Cancel button to close the dialog box, do not press [back] or [search key] to close

Source: Internet
Author: User

Alertdialog.builder builder = new Builder (this);

Builder.create (). Show ();

The dialog box is displayed so that the dialog can be closed when the user presses the back or search key.

How to implement a dialog box similar to that on a Windows system, you must click the button on the dialog box to close the dialog box.

Here is the specific code:

Alertdialog.builder builder = new Builder (this);
Builder.setmessage (msg);
Builder.settitle (title);

Builder.setpositivebutton ("OK", new Dialoginterface.onclicklistener () {
public void OnClick (Dialoginterface dialog, int which)
{
Dialog.dismiss ();

}
});

Alertdialog Alertdialog = Builder.create ();

alertdialog.setcancelable (false); Setting this dialog box cannot be canceled by the user by pressing [Return key], but the test finds that if the user presses Keyevent.keycode_search, the dialog box will be dismiss off.
Due to setting alertdialog.setcancelable (false); found that if the user pressed the Keyevent.keycode_search, the dialog box will still be dismiss off, the setonkeylistener role here is to block the user press Keyevent.keycode_search
Alertdialog.setonkeylistener (New Dialoginterface.onkeylistener () {
@Override
public boolean OnKey (dialoginterface dialog, int keycode, keyevent event)
{
if (keycode = = Keyevent.keycode_search)
{
return true;
}
Else
{
return false; Default return False
}
}
});

Alertdialog.show ();

2012-01-03

Android: Create a alertdialog dialog box, you must press the OK or Cancel button to close the dialog box, do not press [back] or [search key] to close

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.