android AlertDialog对话框

今天来讲讲这个类吧!

说到这个类首先要说说一些跟他有关联的类先!

首相说说AlertDialog.Builder一看这个名字,很快就能联想到这个类是可以用来创建一个AlertDialog对象的。那么我们首先就从这个AlertDialog.Builder这个类入手吧!

看看官网:

public static class

AlertDialog.Builder

extends Object
java.lang.Object
   ↳android.app.AlertDialog.Builder
它有两个构造方法:

Public Constructors
AlertDialog.Builder(Context context)
Constructor using a context for this builder and theAlertDialog it creates.
AlertDialog.Builder(Context context, int theme)
Constructor using a context and theme for this builder and theAlertDialog it creates.

这两个构造函数都是返回AlertDialog这个类的实例。所以我们在创建AlertDialog类时就可以使用这个方法。当然在这个类下有许多方法,比如

setTitle(int titleId)
Set the title using the given resource id.
添加标题的方法;

setView(View view)
Set a custom view to be the contents of the Dialog.
添加视图的方法;

还有一个比较重要的方法:

publicAlertDialog.Builder setPositiveButton (int textId, DialogInterface.OnClickListener listener)
Since: API Level 1

Set a listener to be invoked when the positive button of the dialog is pressed.

Parameters
textId The resource id of the text to display in the positive button
listener The DialogInterface.OnClickListener to use.
Returns
  • This Builder object to allow for chaining of calls to set methods
添加确定的按钮,这个按钮可以带两个参数,第一个是要显示的内容的ID,第二个是一个监听器。好了,说到了监听器,我们不妨来看看这个DialogInterface.OnClickListener这个监听器。查看官方文档。

public static interface

DialogInterface.OnClickListener

android.content.DialogInterface.OnClickListener
Known Indirect Subclasses 这是一个接口,它的间接子类都可以用这个监听器来监听事件的发生。那么我们要做的事情就是重写这个监听器底下的方法:

public abstract voidonClick (DialogInterface dialog, int which)
Since: API Level 1

This method will be invoked when a button in the dialog is clicked.

Parameters
dialog The dialog that received the click.
which The button that was clicked (e.g. BUTTON1) or the position of the item clicked.



这个方法有两个参数,先说后面那一个,是哪一个按钮被按下了。前面那个参数可就有意思了。

它是一个DialogInterface类,我们可以看看这是一个什么东西,请看官网:

public interface

DialogInterface

android.content.DialogInterface
Known Indirect Subclasses 它是一个接口,底下有这么多的子类,学过面向对象的人都可以很快的反应过来这样设计的目的是为什么.对嘛!就是多态的思想了啊。不懂的请补充多态这方面的知识,可以看看马士兵老师的视频!

好了,知识就那么多,想进一步学习的请看官方文档!下面来看具体的一个例子吧!



main.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="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/username"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20.dip"
        android:layout_marginRight="20.dip"
        android:gravity="left"
        android:text="帐号"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/username"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:autoText="false"
        android:capitalize="none"
        android:gravity="fill_horizontal"
        android:scrollHorizontally="true"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:gravity="left"
        android:text="密码"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/password"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:autoText="false"
        android:capitalize="none"
        android:gravity="fill_horizontal"
        android:password="true"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>


DialogTextActivity.java:

package zsc.edu.lian;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
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 DialogTextActivity extends Activity {

    ProgressDialog m_Dialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Dialog dialog = new AlertDialog.Builder(DialogTextActivity.this)
                .setTitle("登录提示")
                .setMessage("这里需要登录!")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        LayoutInflater factory = LayoutInflater
                                .from(DialogTextActivity.this);
                        final View DialogView = factory.inflate(R.layout.main,
                                null);

                        Dialog dlg = new AlertDialog.Builder(
                                DialogTextActivity.this)
                                .setTitle("登录框")
                                .setView(DialogView)
                                .setPositiveButton("确定",
                                        new DialogInterface.OnClickListener() {

                                            @Override
                                            public void onClick(
                                                    DialogInterface dialog,
                                                    int which) {
                                                m_Dialog.show(
                                                        DialogTextActivity.this,
                                                        "请等待...", "正在为你登录...",
                                                        true);

                                                new Thread() {
                                                    public void run() {
                                                        try {
                                                            sleep(3000);
                                                        } catch (Exception e) {
                                                            e.printStackTrace();
                                                        } finally {
                                                            m_Dialog.dismiss();
                                                        }
                                                    }
                                                }.start();
                                            }
                                        })
                                .setNegativeButton("取消",
                                        new DialogInterface.OnClickListener() {

                                            @Override
                                            public void onClick(
                                                    DialogInterface dialog,
                                                    int which) {
                                                DialogTextActivity.this
                                                        .finish();
                                            }
                                        }).create();
                        dlg.show();
                    }
                })
                .setNegativeButton("退出", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        DialogTextActivity.this.finish();
                    }
                }).create();
        dialog.show();
    }
}


一气呵成的代码,中间都木有停顿的!休息下!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值