The development of Android comes with dialog style sometimes does not meet our needs, this time we need to customize the style, call dialog Setview method can customize the layout, the code is as follows
FinalAlertdialog.builder Alertdialogbuilder =NewAlertdialog.builder (context); View Alertdialogview= View.inflate (Context, R.layout.comment_input_layout,NULL); FinalAlertdialog Alertdialog =alertdialogbuilder.create (); Alertdialog.setview (Alertdialogview,0, 0, 0, 0); FinalEditText EditText =(EditText) Alertdialogview.findviewbyid (r.id.etcontent); Alertdialog.show ();//Set dialog animation from the bottomwindow window =Alertdialog.getwindow (); Window.setcontentview (r.layout.comment_input_layout); Window.setgravity (Gravity.bottom); Window.setwindowanimations (R.style.comment_input_dilog_anim_style); WindowManager WindowManager=( Activity) context). Getwindowmanager (); Display Display=Windowmanager.getdefaultdisplay (); Windowmanager.layoutparams LP=Alertdialog.getwindow (). GetAttributes (); Lp.width= (int) (Display.getwidth ());//Set Widthwindow.setattributes (LP); });
View Code
Enter the animation from the bottom through the window setting, which must be set after Dialog.show ()
But after this, if there is edittext in the layout, when the dialog is displayed, does not automatically eject the software disk, so you need to add the following code before the Show () method:
Inputmethodmanager IMM = (Inputmethodmanager) Mcontext.getsystemservice (context.input_method_service); Imm.showsoftinput (Etcontent, inputmethodmanager.result_shown); Imm.togglesoftinput (0, inputmethodmanager.hide_implicit_only);
The Togglesoftinput method is to toggle the display and hide of the soft keyboard, the first parameter cannot be passed inputmethodmanager.show_forced, otherwise the soft keyboard cannot be turned off when dialog is turned off.
I thought it was perfect ... But again, after declaring the EditText, the two buttons were canceled and determined, and the listener was set up, but the listener didn't work, and the button clicked without any reaction. Workaround:
Set the listener event after animating, width, and so on, and then declaring the control with Window.findviewbyid. The specific reason does not know, the feeling is Window.setcontentview (r.layout.comment_input_layout);
The original control was overwritten and the complete code was appended:
FinalAlertdialog.builder Alertdialogbuilder =NewAlertdialog.builder (context); View Alertdialogview= View.inflate (Context, R.layout.comment_input_layout,NULL); FinalAlertdialog Alertdialog =alertdialogbuilder.create (); Alertdialog.setview (Alertdialogview,0, 0, 0, 0); FinalEditText EditText =(EditText) Alertdialogview.findviewbyid (r.id.etcontent); //automatically eject the soft keyboardAlertdialog.setonshowlistener (NewDialoginterface.onshowlistener () { Public voidonshow (Dialoginterface Dialog) {Inputmethodmanager IMM=(Inputmethodmanager) Context.getsystemservice (Context.input_method_service); Imm.showsoftinput (EditText, Inputmethodmanager.result_shown); Imm.togglesoftinput (0, inputmethodmanager.hide_implicit_only); } }); Alertdialog.show (); Window Window=Alertdialog.getwindow (); Window.setcontentview (r.layout.comment_input_layout); Window.setgravity (Gravity.bottom); Window.setwindowanimations (R.style.comment_input_dilog_anim_style); WindowManager WindowManager=( Activity) context). Getwindowmanager (); Display Display=Windowmanager.getdefaultdisplay (); Windowmanager.layoutparams LP=Alertdialog.getwindow (). GetAttributes (); Lp.width= (int) (Display.getwidth ());//Set Widthwindow.setattributes (LP); ImageButton Ibtnclose=(ImageButton) Window.findviewbyid (r.id.ibtnclose); FinalImageButton ibtnright=(ImageButton) Window.findviewbyid (r.id.ibtnright); FinalEditText etcontent =(EditText) Window.findviewbyid (r.id.etcontent); Ibtnclose.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (view view) {Alertdialog.cancel (); } }); Ibtnright.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (view view) {if(ibtnrightclicklistener!=NULL) {ibtnrightclicklistener.sendcomment (Etcontent.gettext (). toString ()); } alertdialog.cancel (); } });View Code
Custom dialog style, auto eject software tray