Popupdialog declares an internal interface Prioritylistener, which declares a callback function Refreshpriorityui, which refreshes the UI display of the activity after a dialog listener event is triggered. This interface is then implemented in the button click event in Mainactivity and passed as a parameter to the Popupdialog constructor, and implements the Refreshpriorityui () method. Not much to say, directly on the code--
Popupdialog Code:
Public classPopupdialog extends Dialog {/** * Custom Dialog Listener*/ Public InterfacePrioritylistener {/** * Callback function to refresh activity's UI display after the dialog listener event is triggered*/ Public voidRefreshpriorityui (Stringstring); } PrivatePrioritylistener Listener; /** * constructor with listener parameters*/ PublicPopupdialog (Context context,inttheme, Prioritylistener Listener) {Super (context, theme); This. Listener =Listener; } protected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.popup_dialog); Button buttonUp=(Button) Findviewbyid (R.ID.BTN_UP); Buttonup.setonclicklistener (NewButton.onclicklistener () {@Override Public voidOnClick (View v) {dismiss (); Listener.refreshpriorityui ("data from: Top"); } }); }}
Mainactivity Code:
Public classMainactivity extends Activity {@Override Public voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); Button Button=(Button) Findviewbyid (R.ID.BTN); Button.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {popupdialog Popupdialog=NewPopupdialog (mainactivity. This, R.style.dialog,NewPopupdialog.prioritylistener () {@Override Public voidRefreshpriorityui (Stringstring) {Toast.maketext (mainactivity. This,string, Toast.length_short). Show (); } }); Popupdialog.setcanceledontouchoutside (true);//Click the area outside the dialog dialog box to disappearwindow window =Popupdialog.getwindow (); Windowmanager.layoutparams LP=window.getattributes (); LP.Y= - -;//set the offset in the vertical directionLp.dimamount = 0f;//the background is not dimmed when the dialog box pops uppopupdialog.show (); } }); }}
Final effect:
After clicking on, Mainactivity will call the Refreshpriorityui () method:
dialog data transfer to activity