[Android] mobile guard custom dialog box layout, android guard
Mobile phone anti-theft page
Click anti-theft on the mobile phone to determine whether to set a password. If no password is set, a dialog box is displayed. If a password has been set, the Enter Password dialog box is displayed.
The password is stored in SharedPreferences, and the data is retrieved for judgment.
Customize a layout file, dialog_setup_password.xml
Root layout width should not be full of Screen
Internal control. The width must be smaller and the center is aligned. android: gravity = "center"
Two side-by-side buttons, OK and cancel, linear layout horizontal orientation
Obtain the AlertDialog. Builder object, and use new Builder (). Parameter: context object
Call the setView (view) method of the Builder object. The parameter is a View object, which is filled by the layout filler.
Call the View. inflate (context, resource, root) method to convert the layout file into a View object, context, resource, and root
Call the show () method of the Builder object
HomeActivity. java
/*** Open the mobile phone anti-theft dialog box */protected void startMobileSec () {String password = sp. getString ("password", ""); // set the password if (TextUtils. isEmpty (password) {AlertDialog. builder builder = new Builder (this); View view = View. inflate (this, R. layout. dialog_setup_password, null); builder. setView (view); builder. show () ;}else {// enter the password }}
Dialog_setup_password.xml
<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "300dp" android: layout_height = "wrap_content" android: gravity = "center_horizontal" android: orientation = "vertical"> <TextView android: background = "#669933" android: textColor = "# fff" android: layout_width = "match_parent" android: layout_height = "40dp" android: gravity = "center" android: text = "set password"/> <EditText android: layout_width = "290dp" android: layout_height = "wrap_content" android: hint = "Enter Password"/> <EditText android: layout_width = "290dp" android: layout_height = "wrap_content" android: hint = "Please confirm password"/> <RelativeLayout android: layout_marginTop = "10dp" android: layout_marginBottom = "10dp" android: gravity = "center_horizontal" android: layout_width = "200dp" android: layout_height = "wrap_content"> <Button android: layout_alignParentLeft = "true" android: background = "#2aabd2" android: textColor = "# fff" android: layout_width = "wrap_content" android: layout_height = "40dp" android: text = "OK"/> <Button android: layout_alignParentRight = "true" android: background = "# eb9316" android: textColor = "# fff" android: layout_width = "wrap_content" android: layout_height = "40dp" android: text = "cancel"/> </RelativeLayout> </LinearLayout>