Problem-solving Android custom dialog cannot be dismiss

Source: Internet
Author: User

Scene:

Click on an item in the ListView to play the custom dialog. When dialog is initialized, a onclicklistener is passed as a parameter to dialog. Click the button set in the layout to dismiss.

Problem:

The two button in the dialog layout is set to listen but the event is not triggered.


Analysis:

1, Button did add the Onclicklistener event, but not triggered, it is possible to click on the button and register the Listener button is not the same object, so click No dismiss.

Code for custom dialog code and interface calls

Dialog's OnCreate method @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstances       Tate);  <span style= "color: #FF0000;" >root =getlayoutinflater (). from (context). Inflate (R.layout.activity_bt_download, NULL, false);</span> MTvT        Itle = (TextView) Root.findviewbyid (r.id.dialog_title);        Mtvcontent = (TextView) Root.findviewbyid (r.id.dialog_content);        Mbtncancel = (Button) Root.findviewbyid (r.id.dialog_button_cancel);        Mbtncancel.setonclicklistener (listener);        Mbtnok = (Button) Root.findviewbyid (R.id.dialog_button_okey);        Mbtnok.setonclicklistener (listener);        Mtvtitle.settext (GetTitle ());    Mtvcontent.settext (GetContent ());    }//Main interface Call method <pre name= "code" class= "Java" >private Mydialog Dialog;        public void Showconfirm () {dialog = new Mydialog (mactivity, R.style.mydialog, Dialogbuttonclicklistener);        Dialog.settitle (R.string.bt_download_dialog_title); dialog.seTcontent (R.string.bt_download_dialog_title); Set its contentview <span style= "color: #FF0000;"        >dialog.setcontentview (r.layout.activity_bt_download);</span> dialog.setcanceledontouchoutside (false);    Dialog.show (); }



It was observed that the main interface performed the Dialog.setcontentview (r.layout.activity_bt_download) and the layout was set. In Dialog's OnCreate method, the layout file is reloaded each time, causing the button to register the event to be the same as the button displayed on the interface .

So just put the dialog in the
Root =getlayoutinflater (). from (context). Inflate (R.layout.activity_bt_download, NULL, FALSE);
Replaced by
root = GetWindow (). Getdecorview ();
This is to get the layout file of the dialog settings, and then get the button above to add listening
.







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.