1. Define the login. xml custom logon view under layout. The Code is as follows:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:baselineAligned="false" ><LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_weight="1" > <TextView android:layout_width="60dp" android:textSize="15sp" android:layout_height="wrap_content" android:text="@string/username" /> <EditText android:id="@+id/username" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="@string/hintname" /></LinearLayout><LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_weight="1" > <TextView android:layout_width="60dp" android:textSize="15sp" android:layout_height="wrap_content" android:text="@string/password" /> <EditText android:id="@+id/password" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="@string/hintpassword" /></LinearLayout> </LinearLayout>
2. Write Java code. Here is the custom dialog box called by a button. The button ID is dialog5
Case R. id. dialog5: layoutinflater Inflater = layoutinflater. from (dialogdemoactivity. this); view = Inflater. inflate (R. layout. login, null); // note that the username and password control username = (edittext) view is obtained from the expanded view. findviewbyid (R. id. username); Password = (edittext) view. findviewbyid (R. id. password); New alertdialog. builder (dialogdemoactivity. this ). settitle ("Custom dialog box "). setview (View) // custom view. setpositivebutton ("OK", new dialoginterface. onclicklistener () {public void onclick (dialoginterface Diener, int which) {string username = username. gettext (). tostring (); string Password = password. gettext (). tostring (); toast. maketext (dialogdemoactivity. this, username + "," + password, 1 ). show ();}}). setnegativebutton ("cancel", new dialoginterface. onclicklistener () {public void onclick (dialoginterface Diener, int which) {// todo auto-generated method stub }}). setcancelable (false ). show (); break;
3. Custom dialog box Effects
4. Click OK. The entered user name and password are displayed.