First, the goal.
When you click on "Phone anti-theft", the Custom Settings Password dialog box is played. If the password is already set, the Custom Input Password dialog box is played.
Second, the code implementation.
1. Create a new XML (named Setpwddialog.xml) file under the Layout folder to set the UI for the custom dialog box.
The new XML file code is as follows;
1<?xml version= "1.0" encoding= "Utf-8"?>2<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"3Android:layout_width= "320dip"4android:layout_height= "Wrap_content"5android:gravity= "Center" >6 7<TextView8Android:id= "@+id/setpwddialog_tv_title"9Android:layout_width= "300dip"Tenandroid:layout_height= "40dip" Oneandroid:gravity= "Center" Aandroid:text= "Set anti-theft password" -Android:textsize= "25dip"/> - the<EditText -Android:id= "@+id/setpwddialog_et_pwd" -Android:layout_width= "300dip" -android:layout_height= "40dip" +android:layout_below= "@id/setpwddialog_tv_title" -Android:hint= "Please enter your password"/> + A<EditText atAndroid:id= "@+id/setpwddialog_et_pwd_conf" -Android:layout_width= "300dip" -android:layout_height= "40dip" -android:layout_below= "@id/setpwddialog_et_pwd" -android:hint= "Confirm Password"/> - in<Button -Android:id= "@+id/setpwddialog_bt_conf" toAndroid:layout_width= "130dip" +android:layout_height= "50dip" -android:layout_below= "@id/setpwddialog_et_pwd_conf" theandroid:layout_marginleft= "10dip" *android:text= "Confirm"/> $ Panax Notoginseng<Button -Android:id= "@+id/setpwddialog_bt_cancel" theAndroid:layout_width= "130dip" +android:layout_height= "50dip" Aandroid:layout_aligntop= "@id/setpwddialog_bt_conf" theandroid:layout_alignright= "@id/setpwddialog_et_pwd_conf" +android:layout_marginright= "10dip" -android:text= "Cancel"/> $ $</RelativeLayout>
View Code
2, the new method in the main interface code (named Showsetpwddialog), used to click "Mobile phone anti-theft" When the custom dialog box. In the New Method (Showsetpwddialog ())
①. Create a new Alertdialog.builder object (named Builder) through new Builder (context context) ;
②. Get a View object (named View) by View.inflate (context context, int resource, ViewGroup root) method. The context in the Inflate method is contextual, in this case the This,int resource Is the ID of the XML resource file to be loaded (in this case, the new XML file (Setpwddialog.xml)), andviewgroup Root is the parent view group (NULL in this case).
③. Alertdialog.builder object (builder) Setview (View view) method to get the View object (view) incoming;
④. Finally, the dialog box is displayed through the Shou () method of the Alertdialog.builder object (builder).
The new Method (Showsetpwddialog) code for the Customize dialog box is as follows:
1 protected void Showsetpwddialog () {2 New Builder (homeactivity. this); 3 View view = View.inflate (thisnull); 4 Builder.setview (view); 5 builder.show (); 6 }
View Code
Android Instance-Mobile security Defender (10)-Customize dialog box