Example 5 of AlertDialog in Android (custom dialog box) and androidalertdialog
In Android development, we often need to pop up some dialogs on the Android interface, such as asking users or asking users to choose. These functions are called the Android Dialog box. The AlertDialog implementation method is the builder mode. Some dialogs defined in AlertDialog often cannot meet our requirements on the dialog box. In this case, we need to use the custom dialog box VIEW to meet our requirements. Here I will customize a login prompt dialog box, shown as follows:
Layout (alertdialog custom login button) interface code:
1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" 3 android: orientation = "vertical" android: layout_width = "match_parent" 4 android: layout_height = "match_parent"> 5 <Button 6 android: text = "custom login" 7 android: layout_width = "match_parent" 8 android: layout_height = "wrap_content" 9 android: id = "@ + id/button5" 10 android: onClick = "login"/> 11 </LinearLayout>
Layout (login_layout logon window) interface:
1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" 3 android: orientation = "vertical" android: layout_width = "match_parent" 4 android: layout_height = "match_parent"> 5 <EditText 6 android: layout_width = "match_parent" 7 android: layout_height = "wrap_content" 8 android: inputType = "text" 9 android: hint = "Enter the user name" 10 android: id = "@ + id/et_username"/> 11 <EditText12 android: layout_width = "match_parent" 13 android: layout_height = "wrap_content" 14 android: inputType = "textPassword" 15 android: hint = "enter the password" 16 android: id = "@ + id/et_password"/> 17 </LinearLayout>
Java function implementation code:
1 public class AlertDialogDemo extends AppCompatActivity {2 private EditText et_username, et_password; 3 @ Override 4 protected void onCreate (@ Nullable Bundle savedInstanceState) {5 super. onCreate (savedInstanceState); 6 setContentView (R. layout. alertdialog); 7} 8 public void login (View v) {9 AlertDialog. builder builder = new AlertDialog. builder (this); 10 builder. setTitle ("login"); 11 // get login_layout12 View view = getLayoutInflater () through the layout filler (). inflate (R. layout. login_layout, null); 13 // get two text edit boxes (the password is not implemented here, only for demonstration) 14 final EditText et_username = (EditText) view. findViewById (R. id. et_username); 15 final EditText et_password = (EditText) view. findViewById (R. id. et_password); 16 builder. setView (view); // set login_layout as dialog box 17 builder. setCancelable (false); // set to disabled 18 // set the front button and perform event processing 19 builder. setPositiveButton ("OK", new DialogInterface. onClickListener () {20 @ Override21 public void onClick (DialogInterface dialogInterface, int I) {22 String name = et_username.getText (). toString (). trim (); 23 String pass = et_password.getText (). toString (). trim (); 24 Toast. makeText (AlertDialogDemo. this, name + "logging on .... ", Toast. LENGTH_SHORT ). show (); 25} 26}); 27 // set the reverse button and perform event processing 28 builder. setNegativeButton ("cancel", new DialogInterface. onClickListener () {29 @ Override30 public void onClick (DialogInterface dialogInterface, int I) {31 Toast. makeText (AlertDialogDemo. this, "cancel Logon", Toast. LENGTH_SHORT ). show (); 32} 33}); 34 builder. show (); // displayed Dialog Box 35} 36}