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 .