Android settings Alertdialog Click the button dialog box does not close (go)

Source: Internet
Author: User

(Transferred from: http://blog.csdn.net/winson_jason/article/details/8485524)

When we use the Android Alertdialog to create the dialog, we will encounter a problem is: we add the button whether it is used Setnegativebutton or Setpositivebutton add button, click on the time, Will close the dialog box, but some of our actual needs, we need to keep this dialog box, such as input check code, password and other check problems, if the user input error, and closed after the dialog box, it is very awkward.

Found on the Internet, the original in the source code has a Boolean variable is the Control dialog box closed or not mshowing variable, we only need to use reflection to change this variable can control our dialog box is not closed. So, we can do this by manually setting the value of the mshowing.

1      Try {2     3Field field =4Dialog.getclass (). Getsuperclass (). Getdeclaredfield ("mshowing");5     6Field.setaccessible (true);7     8Field.set (Dialog,false);//true to close, false to not close9     Ten}Catch(Exception e) { One      A e.printstacktrace (); -      -      } the     

Specific code:

1     2     3     4      NewAlertdialog.builder (Bookstore.getinstance ())5     6 . Settitle (r.getstring ("hint")7     8 . Setmessage ("Cannot open")9     Ten. Setpositivebutton ("OK"),NewDialoginterface.onclicklistener () { One      A       Public voidOnClick (Dialoginterface Dialog,intwhich) { -      -      Try{//the following three sentences control the closing of a bullet box the      -Field field = -Dialog.getclass (). Getsuperclass (). Getdeclaredfield ("mshowing"); -      +Field.setaccessible (true); -      +Field.set (Dialog,true);//true indicates that you want to close A      at}Catch(Exception e) { -      - e.printstacktrace (); -      -      } -      in      -      } to      +}). Show ();

I would add after reference:

If you have an error defining field hints, you can change:

Java.lang.reflect.Field Field = Dialog.getclass (). Getsuperclass (). Getdeclaredfield ("mshowing");

Once the dialog is set, the point cancel will not close, so it must be set to close under the Cancel event:

1 ...2 3 NewAlertdialog.builder ( This)4 . Settitle (TitleID)5 . Setview (layout)6 . Setpositivebutton (Android. R.string.ok,7 NewDialoginterface.onclicklistener () {8  Public voidOnClick (Dialoginterface dialog,9 intwhich) {Ten  One //Click OK button A  - } - }). Setnegativebutton (Android. R.string.cancel, the NewDialoginterface.onclicklistener () { -  - //Click the Cancel button -  Public voidOnClick (Dialoginterface dialog, + intwhich) {     - Try{//the following three sentences control the closing of a bullet box +Java.lang.reflect.Field Field = Dialog.getclass (). Getsuperclass (). Getdeclaredfield ("mshowing"); AField.setaccessible (true); atField.set (Dialog,true);//true indicates that you want to close -  -}Catch(Exception e) { - e.printstacktrace (); - }     - } in  - }) to //null) + . Create (). Show (); -}

Android settings Alertdialog Click the button dialog box does not close (go)

Related Article

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.