zoukankan      html  css  js  c++  java
  • android-基础编程-Dialog

    Dialog是一种常见的控件。

    设置对话框一般步骤如下:

    1.实例化dialog

        由于AlertDialog的构造函数的关系,不能直接实例化,需要利用Builder来实例化,如

         AlertDialog.Builder customizeDialog =new AlertDialog.Builder(this);

    2.设置对话框

        设置除了指基本设置如title,messages,icon外,还指设置对话框的按钮,单选框,列表,复选框,view等。

        customizeDialog.setTitle("我是一个自定义Dialog");

        customizeDialog.setView(dialogView);

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

    3.show

        显示对话框。

    常见的dialog。

    a. 正常的dialog

    private void showNormalDialog(){
            /* @setIcon 设置对话框图标
             * @setTitle 设置对话框标题
             * @setMessage 设置对话框消息提示
             * setXXX方法返回Dialog对象,因此可以链式设置属性
             */
            final AlertDialog.Builder normalDialog =
                    new AlertDialog.Builder(this);
            normalDialog.setIcon(R.drawable.icon);
            normalDialog.setTitle("我是一个普通Dialog");
            normalDialog.setMessage("你要点击哪一个按钮呢?");
            normalDialog.setPositiveButton("确定",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            //...To-do
                        }
                    });
            normalDialog.setNegativeButton("关闭",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            //...To-do
                        }
                    });
            // 显示
            normalDialog.show();
        }

    2.List dialog

    private void showListDialog() {
            final String[] items = { "我是1","我是2","我是3","我是4" };
            AlertDialog.Builder listDialog =
                    new AlertDialog.Builder(this);
            listDialog.setTitle("我是一个列表Dialog");
            listDialog.setItems(items, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // which 下标从0开始
                    // ...To-do
                    Toast.makeText(getApplicationContext(),
                            "你点击了" + items[which],
                            Toast.LENGTH_SHORT).show();
                }
            });
            listDialog.show();
        }

     

    3.单选

    private void showSingleChoiceDialog(){
            final String[] items = { "我是1","我是2","我是3","我是4" };
            yourChoice = -1;
            AlertDialog.Builder singleChoiceDialog =
                    new AlertDialog.Builder(this);
            singleChoiceDialog.setTitle("我是一个单选Dialog");
            // 第二个参数是默认选项,此处设置为0
            singleChoiceDialog.setSingleChoiceItems(items, 0,
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            yourChoice = which;
                        }
                    });
            singleChoiceDialog.setPositiveButton("确定",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            if (yourChoice != -1) {
                                Toast.makeText(getApplicationContext(),
                                        "你选择了" + items[yourChoice],
                                        Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
            singleChoiceDialog.show();
        }

    4.多选

    ArrayList<Integer> yourChoices = new ArrayList<>();
        private void showMultiChoiceDialog() {
            final String[] items = { "我是1","我是2","我是3","我是4" };
            // 设置默认选中的选项,全为false默认均未选中
            final boolean initChoiceSets[]={false,false,false,false};
            yourChoices.clear();
            AlertDialog.Builder multiChoiceDialog =
                    new AlertDialog.Builder(this);
            multiChoiceDialog.setTitle("我是一个多选Dialog");
            multiChoiceDialog.setMultiChoiceItems(items, initChoiceSets,
                    new DialogInterface.OnMultiChoiceClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which,
                                            boolean isChecked) {
                            if (isChecked) {
                                yourChoices.add(which);
                            } else {
                                yourChoices.remove(which);
                            }
                        }
                    });
            multiChoiceDialog.setPositiveButton("确定",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            int size = yourChoices.size();
                            String str = "";
                            for (int i = 0; i < size; i++) {
                                str += items[yourChoices.get(i)] + " ";
                            }
                            Toast.makeText(getApplicationContext(),
                                    "你选中了" + str,
                                    Toast.LENGTH_SHORT).show();
                        }
                    });
            multiChoiceDialog.show();
        }

     

    5.等待中

    private void showWaitingDialog() {
        /* 等待Dialog具有屏蔽其他控件的交互能力
         * @setCancelable 为使屏幕不可点击,设置为不可取消(false)
         * 下载等事件完成后,主动调用函数关闭该Dialog
         */
            ProgressDialog waitingDialog=
                    new ProgressDialog(this);
            waitingDialog.setTitle("我是一个等待Dialog");
            waitingDialog.setMessage("等待中...");
            waitingDialog.setIndeterminate(true);
            waitingDialog.setCancelable(false);
            waitingDialog.show();
        }

     

    6.输入

    private void showInputDialog() {
        /*@setView 装入一个EditView
         */
            final EditText editText = new EditText(this);
            AlertDialog.Builder inputDialog =
                    new AlertDialog.Builder(this);
            inputDialog.setTitle("我是一个输入Dialog").setView(editText);
            inputDialog.setPositiveButton("确定",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Toast.makeText(getApplicationContext(),
                                    editText.getText().toString(),
                                    Toast.LENGTH_SHORT).show();
                        }
                    }).show();
        }

     

    7.进度条

    private void showProgressDialog() {
        /* @setProgress 设置初始进度
         * @setProgressStyle 设置样式(水平进度条)
         * @setMax 设置进度最大值
         */
            final int MAX_PROGRESS = 100;
            final ProgressDialog progressDialog =
                    new ProgressDialog(this);
            progressDialog.setProgress(0);
            progressDialog.setTitle("我是一个进度条Dialog");
            progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            progressDialog.setMax(MAX_PROGRESS);
            progressDialog.show();
        /* 模拟进度增加的过程
         * 新开一个线程,每个100ms,进度增加1
         */
            new Thread(new Runnable() {
                @Override
                public void run() {
                    int progress= 0;
                    while (progress < MAX_PROGRESS){
                        try {
                            Thread.sleep(100);
                            progress++;
                            progressDialog.setProgress(progress);
                        } catch (InterruptedException e){
                            e.printStackTrace();
                        }
                    }
                    // 进度达到最大值后,窗口消失
                    progressDialog.cancel();
                }
            }).start();
        }

    8.自定义

    private void showCustomizeDialog() {
        /* @setView 装入自定义View ==> R.layout.dialog_customize
         * 由于dialog_customize.xml只放置了一个EditView,因此和图8一样
         * dialog_customize.xml可自定义更复杂的View
         */
            AlertDialog.Builder customizeDialog =
                    new AlertDialog.Builder(this);
            final View dialogView = LayoutInflater.from(this)
                    .inflate(R.layout.dialog_customize,null);
            customizeDialog.setTitle("我是一个自定义Dialog");
            customizeDialog.setView(dialogView);
            customizeDialog.setPositiveButton("确定",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // 获取EditView中的输入内容
                            EditText edit_text =
                                    (EditText) dialogView.findViewById(R.id.edit_text);
                            Toast.makeText(getApplicationContext(),
                                    edit_text.getText().toString(),
                                    Toast.LENGTH_SHORT).show();
                        }
                    });
            customizeDialog.show();
        }

     

    
    
  • 相关阅读:
    c语言 ,回调函数[个人理解]
    MFC、C++ 、Windows编程高手
    c++, 虚基派生 : 共同基类产生的二义性的解决办法
    c++,命名空间(namespace)
    c++,纯虚函数与抽象类
    c++ ,protected 和 private修饰的构造函数
    c++ 虚析构函数[避免内存泄漏]
    c++,虚函数
    c++,类的组合
    GPU与CPU的区别
  • 原文地址:https://www.cnblogs.com/zCoderJoy/p/6540450.html
Copyright © 2011-2022 走看看