Dialog的创建

代码来自于APIDemo

 

1. 最简单的OK/Cancel的弹出框

//创建新的弹出框 
new AlertDialog.Builder(AlertDialogSamples.this)
            //设置弹出框的图标
                .setIcon(R.drawable.alert_dialog_icon)
                //弹出框的标题
                .setTitle(R.string.alert_dialog_two_buttons_title)
                //OK按钮的设置,以及对应的onClick事件
                .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
//onClick事件的具体实现
              }
                })
                //Cancel按钮的设置,以及对应的onClick事件 
                .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                     //onClick事件的具体实现
                    }
                })
                .create();

 效果图:

 


2. 带有List的弹出框

//创建带有Item的弹出框 
  return new AlertDialog.Builder(AlertDialogSamples.this)
            //设置弹出框的标题
                .setTitle(R.string.select_dialog)
                //设置item的具体内容,R.array.select_dialog_items是定义在XML中的文件,也可以是new String[]{"a","b" ……}的数组
                .setItems(R.array.select_dialog_items, new DialogInterface.OnClickListener() {
                //选中Item时产生的事件
                    public void onClick(DialogInterface dialog, int which) {
                        /* User clicked so do some stuff */
                        String[] items = getResources().getStringArray(R.array.select_dialog_items);
                        new AlertDialog.Builder(AlertDialogSamples.this)
                                .setMessage("You selected: " + which + " , " + items[which])
                        //直接显示,显示之前用户没有什么其它的额外操作        
                                .show();
                    }
                })
                //显示之前,用户可能有额外的操作
                .create();

 

 对应的XML文件:

 

<xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- Used in app/dialog examples -->
    <string-array name="select_dialog_items">
        <item>Command one</item>
        <item>Command two</item>
        <item>Command three</item>
        <item>Command four</item>
    </string-array>
</resources>

效果图:

 

 

3. 进度条式的弹出框的实现//创建一个进度条式的弹出框

            mProgressDialog = new ProgressDialog(AlertDialogSamples.this);
            //设置图标
            mProgressDialog.setIcon(R.drawable.alert_dialog_icon);
            //设置标题
            mProgressDialog.setTitle(R.string.select_dialog);
            //设置进度条的显示方式(水平的)
            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            //设置进度条的最大值
            mProgressDialog.setMax(MAX_PROGRESS);
            //设置按钮
            mProgressDialog.setButton(getText(R.string.alert_dialog_hide), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                }
            });
            mProgressDialog.setButton2(getText(R.string.alert_dialog_cancel), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                }
            });

效果图:

 

 

4. 单选式弹出框的实现

 

	//单选按钮式的弹出框
            return new AlertDialog.Builder(AlertDialogSamples.this)
                .setIcon(R.drawable.alert_dialog_icon)
                .setTitle(R.string.alert_dialog_single_choice)
                //设置单选按钮的集合,同理该处可以来自于XML,也可以是String[]{}的实现
                .setSingleChoiceItems(R.array.select_dialog_items2, 0, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                    }
                })
                .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                    }
                })
                .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                    }
                })
               .create();

效果图:

 

 

 

5. 多选弹出框的实现

 

//其道理与上相同,只是一个单选一个多选 
return new AlertDialog.Builder(AlertDialogSamples.this)
                .setIcon(R.drawable.ic_popup_reminder)
                .setTitle(R.string.alert_dialog_multi_choice)
                .setMultiChoiceItems(R.array.select_dialog_items3,
                        new boolean[]{false, true, false, true, false, false, false},
                        new DialogInterface.OnMultiChoiceClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton,
                                    boolean isChecked) {
                            }
                        })
                .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                    }
                })
                .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                    }
                })
               .create();

效果图:

 

 

 

6. 自定义布局弹出框 

//用于加载布局文件

 LayoutInflater factory = LayoutInflater.from(this);
            //从XML中加载布局
            final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);
            return new AlertDialog.Builder(AlertDialogSamples.this)
                .setIcon(R.drawable.alert_dialog_icon)
                .setTitle(R.string.alert_dialog_text_entry)
                //关键在这里,将这个布局加入到这个弹出框中
                .setView(textEntryView)
                .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        /* User clicked OK so do some stuff */
                    }
                })
                .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                        /* User clicked cancel so do some stuff */
                    }
                })
                .create();

效果图:

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值