Step 1:
First set the login screen- dialog_login.xml : username and password input box, and Clear button and login button
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"android:orientation= "vertical"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:padding= "8DP"> <TablelayoutAndroid:id= "@+id/dl_table"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:stretchcolumns= "1"android:visibility= "visible"Tools:layout_editor_absolutex= "8DP"Tools:layout_editor_absolutey= "8DP"> <TableRow> <TextViewAndroid:layout_width= "60DP"Android:layout_height= "Wrap_content"android:gravity= "Center"Android:text= "User" /> <EditTextAndroid:id= "@+id/et_user"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content" /> </TableRow> <TableRowAndroid:layout_width= "Match_parent"Android:layout_height= "Match_parent"> <TextViewAndroid:layout_width= "60DP"Android:layout_height= "Wrap_content"android:gravity= "Center"Android:text= "Password" /> <EditTextAndroid:id= "@+id/et_password"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:inputtype= "Textpassword" /> </TableRow> </Tablelayout> <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"> <ButtonAndroid:id= "@+id/btn_clear"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:layout_weight= "1"Android:text= "Clear" /> <ButtonAndroid:id= "@+id/btn_login"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:layout_weight= "1"Android:text= "Login" /> </LinearLayout>
Step 2:
1. Load the Dialog_login.xml interface into the generated View object;
2. Build dialog the object and load the custom view in;
3. Display the login dialog box;
public class customactivity extends appcompatactivity {@Override protected void OnCreate ( Bundle savedinstancestate) { super = View.inflate (this , R.layout.dialog_login, null ); Alertdialog.builder Builder = new alertdialog.builder (this ). Setview (view); Builder.show (); }}
Note: The call builder.show is equivalent to creating first AlertDialog and then displaying
This completes the Login dialog box creation, if you want to further get user input information, click the button can be manipulated, add the following code:
Android alertdialog-Login dialog box