Android dialog box Introduction 2 custom dialog box

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.