Android custom alertdialog dialog dialog style
In actual project development, it is often necessary to define alertdialog according to actual requirements. Recently, a WiFi connection function is being developed. Clicking WiFi needs to pop up a user-defined password input box. This right shall be recorded here
design sketch
Click the button on the home page to pop up the dialog box, displaying WiFi information (textview), password input box (EditText), cancel and connect button (button)
realization
Create a layout for alertdialog according to your actual needs. Here, I need to define a WiFi password input box as shown in the figure, so create a dialog in the RES / layout directory_ Layout.xml file.
In this layout, define a textview to display WiFi name, a split line, an EditText for password input, and two buttons to cancel connection with
Create a new wifidialog.java, inherit alertdialog, and introduce the dialog just defined_ Layout. XML layout, and do our logical operation here
Declare construction method, pass in context
Load the layout in oncreate(), get the view, and set the click event for the button
One particular problem to pay attention to here is that after defining EditText in the dialog, click EditText in the pop-up box, and the keyboard will not pop up for input. Therefore, use this. Getwindow(). Clearflags (WindowManager. Layoutparams. Flag_alt_focusable_im) to ensure that the keyboard can pop up for password input
It's easy to call. New a WiFi dialog object and call the show () method. Here, simply declare a button in mainactivity, set the click event, and a dialog box will pop up.