关于android:最全的-Android-之-Dialog

6次阅读

共计 8261 个字符,预计需要花费 21 分钟才能阅读完成。

Android 之 Dialog

一、简介

对话框的性能次要就是提醒一些信息给用户,让用户可进行下一步操作,或者提醒用户该操作不可逆等等。

然而对话框自身的时候不简单,简单在于和其余控件联合起来应用。

上面介绍会介绍几种罕用的对话框。

二、属性和办法

Android 零碎提供的对话框父类为 Dialog, 外面并没有实现对话框的具体类型,比方单选、多选、列表等对话框,仅提供一个框架和标准。零碎为开发者提供了一个 多功能的对话框类 AlertDialog, 封装了各种对话框的款式,咱们只须要实现要显示的内容和监听。

​ 大部分对话框就是应用零碎封装好的对话框 AlertDialog 的实例对象。AlertDialog 并不提供对外的构造方法,即不能间接通过 AlertDialog 的构造函数来生产一个 AlertDialog。因为 AlertDialog 所有的构造方法都是 protected 的。所以为了获取 AlertDialog 对象,零碎提供了一个动态外部类 Builder 让咱们应用,通过 Builder 能够创立 AlertDialog 对象。

Builder 对象设置对话框的属性

        .setTitle() // 设置题目
        .setIcon ()// 设置图标
       .setMessage ()// 设置要显示的内容
       .setItems// 设置在对话框中显示的我的项目列表
       .setView// 设置自定义的对话框款式
       .setPositiveButton ()// 设置确定按钮
       .setNegativeButton ()// 设置勾销按钮
       .setNeutralButton ()// 设置中立按钮
       .setSingleChoiceItems// 单选框
       .setMultiChoiceItems// 复选框

三、各种 Dialog 的应用

对话框的在安卓中的应用堪称无处不在,应用十分的宽泛,一个好的对话框能进步用户的应用感。这在安卓开发中是十分重要的。

本章节会介绍以下这些常见的对话框的应用。

应用步骤:

  1. 创立 AlertDialog.Builder 实例对象。
  2. 通过 Builder 对象设置对话框的属性。
  3. 调用 Builder 对象的 create()办法创立 AlertDialog 对话框
  4. 调用 AlertDialog 的 show()办法来显示对话框
  5. 调用 AlertDialog 的 dimiss()办法销毁对话框。

1、一般的对话框

当咱们要提醒用户一些信息,以及让用户做一些判断的选项时候,就能够时候简略的对话框即可。

如图成果:

java 外围代码:

AlertDialog dialog=new AlertDialog.Builder(this)
                        .setIcon(R.drawable.hmbb)// 设置图标
                        .setTitle("我是一般的对话框")// 设置题目
                        .setMessage("我是内容")// 设置要显示的内容
                        .setNegativeButton("勾销", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {Toast.makeText(MainActivity.this, "点击了勾销按钮", Toast.LENGTH_SHORT).show();
                                dialogInterface.dismiss();// 销毁对话框}
                        })
                    .setNeutralButton("第三方按钮", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {Toast.makeText(MainActivity.this, "我是第三个按钮", Toast.LENGTH_SHORT).show();}
                        })
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {Toast.makeText(MainActivity.this, "点击了确定的按钮", Toast.LENGTH_SHORT).show();
                                dialog.dismiss();// 销毁对话框}
                        }).create();//create()办法创立对话框
                dialog.show();// 显示对话框

2、列表对话框

如图成果:

java 外围代码:

final String hobby[] = {"吃饭", "睡觉", "敲代码", "看电视","打游戏"};
                AlertDialog dialog1=new AlertDialog.Builder(this)
                        .setIcon(R.drawable.hmbb)
                        .setTitle("请选出你最喜爱的喜好")
                        .setItems(hobby, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {Toast.makeText(MainActivity.this, hobby[i], Toast.LENGTH_SHORT).show();}
                        })
                        .setNegativeButton("勾销", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {dialog.dismiss();
                            }
                        })
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {dialog.dismiss();
                            }
                        }).create();
                dialog1.show();

3、单选对话框

成果如图:

外围 java 代码:

             final String[] gaame = new String[]{"王者光荣", "LOL", "QQ 飞车", "地下城","阴阳师"};
                AlertDialog dialog2=new AlertDialog.Builder(this)
                        .setIcon(R.drawable.game)
                        .setTitle("请抉择你最喜爱的游戏")
                        // 第一个参数: 设置单选的资源; 第二个参数: 设置默认选中哪几项
                        .setSingleChoiceItems(gaame, 0, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int i) {Toast.makeText(MainActivity.this, gaame[i], Toast.LENGTH_SHORT).show();}
                        })
                        .setNegativeButton("勾销", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {dialog.dismiss();
                            }
                        })
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {dialog.dismiss();
                            }
                        }).create();
                dialog2.show();

4、多选对话框

成果如图:

外围 java 代码:

    final String items[] = {"篮球", "足球", "排球", "乒乓球","羽毛球","跑步"};
                final boolean checkedItems[] = {true, false, false, false,true,true};
                AlertDialog dialog3 = new AlertDialog.Builder(this)
                        .setIcon(R.drawable.hmbb)// 设置题目的图片
                        .setTitle("抉择你喜爱的静止")// 设置对话框的题目
                        // 第一个参数: 设置单选的资源; 第二个参数: 设置默认选中哪几项
                        .setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int i, boolean isChecked) {checkedItems[i] = isChecked;
                            }
                        })
                        .setNegativeButton("勾销", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {dialog.dismiss();
                            }
                        })
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {for (int i = 0; i < checkedItems.length; i++) {if (checkedItems[i]) {Toast.makeText(MainActivity.this, "选中了" + i, Toast.LENGTH_SHORT).show();}
                                }
                                dialog.dismiss();}

                        }).create();
                dialog3.show();

5、进度条对话框

ProgressDialog 也是继承于 Dialog, 但其扩大了缓冲加载提醒的性能。

成果如图:

java 外围代码:

// 实例化一个 ProgressDialog 进度条对话框对象
final ProgressDialog progressDialog=new ProgressDialog(this);
  // 设置进度条为程度款式
  progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                progressDialog.setMessage("加载中");
                progressDialog.setMax(100);
                final Timer timer=new Timer();
                        timer.schedule(new TimerTask() {
                    int startprogress=0;
                    @Override
                    public void run() {startprogress+=new Random().nextInt(10);
                        progressDialog.setProgress(startprogress);
                        if (startprogress>100)
                            timer.cancel();}
                },0,1000);

            progressDialog.show();
            break;

如果要是实现旋转的进度条的话

java 外围代码:

    ProgressDialog progressDialog1 = new ProgressDialog(this);
                progressDialog1.setMessage("正在加载中");
                progressDialog1.show();

6、日期抉择对话框

成果如图:

java 外围代码:

Calendar data=Calendar.getInstance();
                DatePickerDialog dpd=new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int day) {
                        String str;
                        str = String.valueOf(year) + "-" + String.valueOf(month) + "-" + String.valueOf(day);
                        Toast.makeText(getApplicationContext(), str, Toast.LENGTH_LONG).show();}

                },data.get(Calendar.YEAR),data.get(Calendar.MONTH),data.get(Calendar.DAY_OF_MONTH));
                dpd.show();

7、工夫抉择对话框

成果如图:

java 外围代码:

Calendar calendar=Calendar.getInstance();
                new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {}},
                    calendar.get(Calendar.HOUR_OF_DAY),
                    calendar.get(Calendar.MINUTE),
                    true
                ).show();

8、自定义对话框

自定义对话框次要是应用这个 .setView(view) 办法,加载一个咱们自定义的布局。

成果如下:

首先咱们须要将零碎 Dialog 默认的款式去掉,防止影响咱们的款式布局

在 res->values->style.xml 文件中增加咱们去除零碎对话框的款式

 <!-- 对话框的款式 -->
    <style name="NormalDialogStyle">
        <!-- 对话框背景 -->
        <item name="android:windowBackground">@android:color/transparent</item>
        <!-- 边框 -->
        <item name="android:windowFrame">@null</item>
        <!-- 没有题目 -->
        <item name="android:windowNoTitle">true</item>
        <!-- 是否浮现在 Activity 之上 -->
        <item name="android:windowIsFloating">true</item>
        <!-- 背景通明 -->
        <item name="android:windowIsTranslucent">false</item>
        <!-- 是否有笼罩 -->
        <item name="android:windowContentOverlay">@null</item>
        <!-- 背景变暗 -->
        <item name="android:backgroundDimEnabled">true</item>
    </style>

编写自定义对话框 div_dialog.xml 的布局代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#3CC4C4"
        android:text="点击登录" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:background="#ddd" />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#D1FAF2"
        android:text="集体核心" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:background="#ddd" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#3CC4C4"
        android:text="我的珍藏" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:background="#ddd" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#D1FAF2"
        android:text="我的珍藏" />


    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="#2BD5D5"
        android:text="勾销" />

    <View
        android:layout_width="match_parent"
        android:layout_height="15dp" />
</LinearLayout>

java 外围代码:

 // 实例化 Dialog 对象,并利用 NormalDialogStyle 去掉零碎款式
                Dialog dialog4 = new Dialog(this,R.style.NormalDialogStyle);
                View view = View.inflate(this, R.layout.div_dialog, null);
                dialog4.setContentView(view);// 给 dialog 设置一个视图
                dialog4.setCanceledOnTouchOutside(true);// 在点击窗口外的中央能够退出
                Window dialogWindow = dialog4.getWindow();// 获取 Dialog 的窗口
                // 实例化窗口布局对象
                WindowManager.LayoutParams lp = dialogWindow.getAttributes();
                lp.width =WindowManager.LayoutParams.MATCH_PARENT;// 宽度铺满
                lp.height = WindowManager.LayoutParams.WRAP_CONTENT;// 高度自适应
                lp.gravity = Gravity.BOTTOM;// 窗口停靠在底部居中
                dialogWindow.setAttributes(lp);
                dialog4.show();

罕用的对话框都介绍的差不多了。:.゚ヽ(。◕‿◕。)ノ゚.:。+゚

点个赞再走吧٩(๑´0`๑)۶。奥里给!

正文完
 0