您现在的位置:首页 > 博客 > Android开发 > 正文
AlertDialog对话框实例
http://www.drovik.com/      2012-9-5 12:24:57      来源:Android软件开发      点击:

Android中实现对话框可以使用AlertDialog.Builder类,还可以自定义对话框.如果对话框设置了按钮,需要设置监听OnClickListener.

在res/layout下建dialog.xml
<?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:
        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:
        android:capitalize="none"
        android:gravity="fill_horizontal"
        android:password="true"
        android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>

使用AlertDialog创建对话框
package com.yarin.android.Examples_04_14;

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;
  
    @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();
    }
}

 

分享到:
发表评论(0)
姓名 *
评论内容 *
验证码 *图片看不清?点击重新得到验证码