首篇 sdk 之 AlertDialog

带着十足的干劲,用着有限的英语水平,我们来看看sdk里docs里的AlertDialog:

AlertDialog  

  SDK 原文描述:A dialog that can show a title, up to three buttons, a list of selectable items, or a custom layout.

 一个能够展示标题,多达三个按钮,一系列可选择条目的对话框。或者使用自定义的布局。

接下来就开始使用它:

  首先先声明一点,Dialog下的子类包括它本身,都是依附于一个Activity的生命,所以在创建一个Dialog时传递的上下文一定是某个Activity.this,而不是getApplicationContent(),切记这一点。

第一个用法也是最常用的:实现效果

package com.aimqq.alertdialogsdk;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.view.View;
import android.widget.Toast;

public class NormalDialog extends Activity {

    private AlertDialog.Builder builder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_normal_dialog);
        builder = new Builder(NormalDialog.this);
        builder.setTitle("AlertDialog");
        builder.setMessage("欢迎来到aimqq博客");
        builder.setPositiveButton("确定", new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getApplicationContext(), "点击确定按钮",
                        Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("取消", new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), "点击取消按钮",
                        Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void show(View v) {
        //这种方式也可以显示
//        builder.show();
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }
}

 这里说一个可能会有的疑问,为什么不直接 new AlertDialog(context)呢?因为你new不出来,她的三个构造方法都是protected!!

第二种用法:添加一个条目

listBuilder = new Builder(NormalDialog.this);
        listBuilder.setTitle("选择一种颜色");
        String[] items = new String[] { "红色", "绿色", "蓝色", "黄色" };
        listBuilder.setItems(items, new OnClickListener() {
            // 参1 就是当前的这个 dialog 参2 就是被点击的那个条目
            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (which) {
                case 0:
                    Toast.makeText(getApplicationContext(), "我是红色",
                            Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    Toast.makeText(getApplicationContext(), "我是绿色",
                            Toast.LENGTH_SHORT).show();
                    break;
                case 2:
                    Toast.makeText(getApplicationContext(), "我是蓝色",
                            Toast.LENGTH_SHORT).show();
                    break;
                case 3:
                    Toast.makeText(getApplicationContext(), "我是黄色",
                            Toast.LENGTH_SHORT).show();
                    break;
                }
            }
        });

 

第四种用法:添加多选条目或者单选条目

   

multBuilder = new Builder(NormalDialog.this);
        multBuilder.setTitle("选择你喜欢的颜色");
        boolean[] checkedItems = new boolean[]{false,true,false,false}; //默认选中绿色
        //参1 要显示的条目集   参2默认那些条目选中   参3 监听条目
        multBuilder.setMultiChoiceItems(items, checkedItems , new OnMultiChoiceClickListener() {
            
            //参3 isChecked True if the click checked the item, else false.
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                //这里可根据自身需要,做相应的逻辑处理
            }
        });
        multBuilder.setPositiveButton("确定", new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        multBuilder.setNegativeButton("取消", new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });

单选:

   

multBuilder.setSingleChoiceItems(items, 0, null);
        multBuilder.setNegativeButton("取消", new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        
        multBuilder.setPositiveButton("确定", new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });

 

第五种用法:创建一个自定义的AlterDialog

  You can then call setView() to place the layout in the dialog. 你可以使用setVIew()方法将局部添加到dialog

Activity中的代码:

multBuilder = new Builder(NormalDialog.this);
        View view = View.inflate(NormalDialog.this, R.layout.dialog_cust, null);
        multBuilder.setView(view);
        // 可使用view对象获取控件做相应的逻辑操作
        multBuilder.setNegativeButton("取消", new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });

        multBuilder.setPositiveButton("登录", new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });

布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <ImageView
        android:src="@drawable/ic_launcher"
        android:layout_width="match_parent"
        android:layout_height="64dp"
        android:scaleType="center"
        android:background="#FFFFBB33"
        android:contentDescription="@string/app_name" />
    <EditText
        android:id="@+id/username"
        android:inputType="textEmailAddress"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="4dp"
        android:layout_marginBottom="4dp"
        android:hint="请输入账号" />
    <EditText
        android:id="@+id/password"
        android:inputType="textPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dp"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="4dp"
        android:layout_marginBottom="16dp"
        android:fontFamily="sans-serif"
        android:hint="请输入密码"/>
</LinearLayout>

这基本就是AlertDialog使用的集中情况况,如果有遗漏或者错误请反馈一下,谢谢。

 再顺便简单提一下,因为没有使用太多,就简单说一下

可以将Acitivity设置成Dialog使用,通过设置acitivity的主题就可以实现:

<activity android:theme="@android:style/Theme.Holo.Dialog" >
posted @ 2016-09-09 00:11  Spiderman.L  阅读(253)  评论(0编辑  收藏  举报