Android之Dialog详解

DialogAndroid中最常用的组件之一,类似黑莓中的Dialog

Android中的alertDialog的创建一般是通过其内嵌类AlertDialog.Builder来实现的。

先来看下builder的方法:

setTitle():给对话框设置title

setIcon():给对话框设置图标

setMessage():设置对话框的提示信息

setItems():设置对话框要显示的一个list,一般用于要显示几个命令时

setSingleChoiceItems():设置对话框显示一个单选的List

setMultiChoiceItems():用来设置对话框显示一系列的复选框

setPositiveButton():给对话框添加Yes按钮

setNegativeButton():给对话框添加No按钮

setNeutralButton:给对话框添加"中立"按钮

Android中的Dialog包括了以下类型:

警告对话框:Alertialog

进度对话框:ProgressDialog

日期选择对话框:DatePickerDialog

时间选择对话框:TimePickerDialog

自定义对话框:继承Dialog,重写相关方法

Android中创建Dialog有两种方式:

1、直接new一个Dialog对象,然后调用Dialog对象的showdismiss方法来控制对话框的显示和隐藏。

2、在ActivityonCreateDialog(int id)方法中创建Dialog对象并返回,然后调用ActivtyshowDialog(int id)dismissDialog(int id)来显示和隐藏对话框。

二者之间的区别:

通过第二种方式创建的对话框会继承Activity的属性,比如可以获得Activitymenu事件等。

 

AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog.this);

setIcon

setTitle

setPositiveButton

setNeutralButton

setNegativeButton

setMessage

final String[] mItems = {"item0","item1","itme2","item3","itme4","item5","item6"};

builder.setItems(mItems, new DialogInterface.OnClickListener() {

     public void onClick(DialogInterface dialog, int which) {

         showDialog("你选择的id" + which + " , " + mItems[which]);

     }

  });

builder.create().show();

builder.setSingleChoiceItems(mItems, 0, new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int whichButton) {

mSingleChoiceID = whichButton;

showDialog("你选择的id" + whichButton + " , " + mItems[whichButton]);

}

});

ProgressDialog mProgressDialog = new ProgressDialog(MainDialog.this);

        mProgressDialog.setIcon(R.drawable.icon);

        mProgressDialog.setTitle("进度条窗口");

        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

        mProgressDialog.setMax(MAX_PROGRESS);

        mProgressDialog.setButton("确定", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int whichButton) {

                //这里添加点击后的逻辑

            }

        });

        mProgressDialog.setButton2("取消", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int whichButton) {

                //这里添加点击后的逻辑

            }

 

        });

        mProgressDialog.show();

        new Thread(this).start();

        public void run() {

        int Progress = 0;

        while(Progress < MAX_PROGRESS) {

        try {

        Thread.sleep(100);

        Progress++;  

        mProgressDialog.incrementProgressBy(1);

        } catch (InterruptedException e) {

        e.printStackTrace();

        }

        }

        }

MultiChoiceID 用于记录多选选中的id号 存在ArrayList选中后 add ArrayList取消选中后 remove ArrayList

ArrayList <Integer>MultiChoiceID = new ArrayList <Integer>();

AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog.this);

MultiChoiceID.clear();

builder.setIcon(R.drawable.icon);

builder.setTitle("多项选择");

builder.setMultiChoiceItems(mItems,

new boolean[]{false, false, false, false, false, false, false},

new DialogInterface.OnMultiChoiceClickListener() {

public void onClick(DialogInterface dialog, int whichButton, boolean isChecked) {

if(isChecked) {

MultiChoiceID.add(whichButton);

showDialog("你选择的id" + whichButton + " , " + mItems[whichButton]);

}else {

MultiChoiceID.remove(whichButton);

}

}

});

builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int whichButton) {

String str = "";

int size = MultiChoiceID.size();

for (int i = 0 ;i < size; i++) {

str+= mItems[MultiChoiceID.get(i)] + ", ";

}

showDialog("你选择的是" + str);

}

);

builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int whichButton) {

}

});

builder.create().show();

转载自: http://www.apkbus.com/android-13412-1-2.html

posted @ 2012-12-11 09:23  JD.Tang  阅读(353)  评论(0编辑  收藏  举报