Android Let Alertdialog Click OK won't disappear

Source: Internet
Author: User

In Android Alertdialog, you can close the dialog box by using the button that comes with it, clicking OK or Cancel. However, we sometimes hope that when we click OK to find that he has not filled out the option, we prompt him to fill in the complete, this time do not close the dialog box. Of course, there are a lot of solutions. Like using an activity to do dialog. or customize view. Use the button you have defined. Do not use the button that comes with it. But sometimes our dialog view is simple, and we continue to use our own button at this time. What to do at this time. Can take advantage of Java's reflection technology. Place the button where it needs not to be closed:

try {Field field = Dialog.getclass (). Getsuperclass (). Getdeclaredfield ("mshowing"); Field.setaccessible (true);// Set the mshowing variable to false to indicate that the dialog box is closed Field.set (dialog, false);d Ialog.dismiss ();} catch (Exception e) {e.printstacktrace ();}

Where you need to close:

try {Field field = Dialog.getclass (). Getsuperclass (). Getdeclaredfield ("mshowing"); Field.setaccessible (true);// Set the mshowing variable to false to indicate that the dialog box is closed Field.set (dialog, True);d Ialog.dismiss ();} catch (Exception e) {e.printstacktrace ();}

Here's the Field where the package is Java.lang.reflect.Field

Refer to this article for specific details: http://blog.csdn.net/notonlyforshe/article/details/7955323

Android Let Alertdialog Click OK won't disappear

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.