Block Alertdialog shutdown Instance code in Android _android

Source: Internet
Author: User
Tags reflection

How does Android Close the Alertdialog.builder dialog box? The Alertdialog.builder dialog box does not have a method like finish () or dismiss ().

But its parent class Alertdialog has a dismiss method, and Alertdialog.builder will get a Alertdialog object in the. Show (), and we can use the dismiss method to turn the builder off.

Alertdialog.builder Builder = new Alertdialog.builder (this); 
Alertdialog dialog = Builder.show (); 

About Android block Alertdialog shutdown The specific code looks like this:

Alertdialog.builder Builder = new Alertdialog.builder (getactivity ());
Builder.settitle ("test");
Layoutinflater inflater = getactivity (). Getlayoutinflater ();
View view = Inflater.inflate (r.layout.dialogfragment_num_input, NULL);
Builder.setview (view); Builder.setpositivebutton (OK), new Dialoginterface.onclicklistener () {@Override public void OnClick (dialoginterface dialog, int id) {Field field = null; try {//Get the Private property in dialog by reflection mshowing Field = Dialog.getclass (). Getsuperclass (). getdecl
Aredfield ("mshowing");
Field.setaccessible (TRUE);/Set this property to access the} catch (Exception ex) {} String Inputvalue = string.valueof (Medit.gettext ()); if (Inputvalue = null | |
"". Equals (Inputvalue)) {try {//Set Dialog not close Field.set (dialog, false);
Dialog.dismiss ();  ' Catch (Exception ex) {}} ' else {////Do your own thing//try {//Close Field.set (dialog, True); Dialog.dismiss ();} catch (Exception
ex) {}}}}); Builder.setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {@Override public void OnClick (DialoginterfAce dialog, int id) {field Field = null; try {//Gets the private property in dialog by reflection mshowing Field = Dialog.getclass (). Getsuperclass (). Get
Declaredfield ("mshowing"); Field.setaccessible (TRUE);/Set this property to access the} catch (Exception ex) {} try {Field.set (dialog, True); Dialog.dismiss (); catch (Exception ex)
{
}
}
}); Builder.create ();

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.