首页 > 代码库 > Android-Dialog对话框Demo

Android-Dialog对话框Demo

代码

package com.lxt008;import android.app.Activity;import android.app.AlertDialog;import android.app.Dialog;import android.app.ProgressDialog;import android.content.DialogInterface;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;public class Activity01 extends Activity {    ProgressDialog m_Dialog;    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        Dialog dialog = new AlertDialog.Builder(Activity01.this)            .setTitle("登陆提示")//设置标题            .setMessage("这里需要登录!")//设置内容            .setPositiveButton("确定",//设置确定按钮            new DialogInterface.OnClickListener()             {                public void onClick(DialogInterface dialog, int whichButton)                {                    //点击“确定”转向登陆框                                        LayoutInflater factory = LayoutInflater.from(Activity01.this);                    //得到自定义对话框                    final View DialogView = factory.inflate(R.layout.dialog, null);                    //创建对话框                    AlertDialog dlg = new AlertDialog.Builder(Activity01.this)                    .setTitle("登录框")                    .setView(DialogView)//设置自定义对话框的样式                    .setPositiveButton("确定", //设置"确定"按钮                    new DialogInterface.OnClickListener() //设置事件监听                    {                        public void onClick(DialogInterface dialog, int whichButton)                         {                            //输入完成后,点击“确定”开始登陆                            m_Dialog = ProgressDialog.show                                       (                                         Activity01.this,                                         "请等待...",                                         "正在为你登录...",                                          true                                       );                                                        new Thread()                            {                               public void run()                              {                                 try                                {                                   sleep(3000);                                }                                catch (Exception e)                                {                                  e.printStackTrace();                                }                                finally                                {                                    //登录结束,取消m_Dialog对话框                                    m_Dialog.dismiss();                                }                              }                            }.start();                         }                    })                    .setNegativeButton("取消", //设置“取消”按钮                    new DialogInterface.OnClickListener()                     {                        public void onClick(DialogInterface dialog, int whichButton)                        {                            //点击"取消"按钮之后退出程序                            Activity01.this.finish();                        }                    })                    .create();//创建                    dlg.show();//显示                }            }).setNeutralButton("退出",             new DialogInterface.OnClickListener()             {            public void onClick(DialogInterface dialog, int whichButton)            {                //点击"退出"按钮之后推出程序                Activity01.this.finish();            }        }).create();//创建按钮        // 显示对话框        dialog.show();            }}

 

布局文件1

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    ><TextView      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="@string/hello"    /></LinearLayout>

 

布局文件2

<?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="wrap_content"    android:orientation="vertical">    <TextView         android:id="@+id/username"        android:layout_height="wrap_content"        android:layout_width="wrap_content"        android:layout_marginLeft="20dip"        android:layout_marginRight="20dip"        android:text="账号"        android:gravity="left"        android:textAppearance="?android:attr/textAppearanceMedium" />                <EditText        android:id="@+id/username"        android:layout_height="wrap_content"        android:layout_width="fill_parent"        android:layout_marginLeft="20dip"        android:layout_marginRight="20dip"        android:scrollHorizontally="true"        android:autoText="false"        android:capitalize="none"        android:gravity="fill_horizontal"        android:textAppearance="?android:attr/textAppearanceMedium" />    <TextView        android:id="@+id/password"        android:layout_height="wrap_content"        android:layout_width="wrap_content"        android:layout_marginLeft="20dip"        android:layout_marginRight="20dip"        android:text="密码"        android:gravity="left"        android:textAppearance="?android:attr/textAppearanceMedium" />                <EditText        android:id="@+id/password"        android:layout_height="wrap_content"        android:layout_width="fill_parent"        android:layout_marginLeft="20dip"        android:layout_marginRight="20dip"        android:scrollHorizontally="true"        android:autoText="false"        android:capitalize="none"        android:gravity="fill_horizontal"        android:password="true"        android:textAppearance="?android:attr/textAppearanceMedium" /> </LinearLayout>

 

Android-Dialog对话框Demo