Dialog的使用

Dialog的使用可以簡(jiǎn)單分為:1.使用原生方法(多為AlertDialog.Builder所提供)。2.自定義Dialog。

Screenshot_2019-06-16-23-19-27_副本.png
Screenshot_2019-06-16-23-19-27_副本2.png
Screenshot_2019-06-16-23-19-27_副本3.png
需要注意的是,Dialog是自帶背景的,根據(jù)不同的Theme可以有不同顏色的背景,如上圖,在使用有背景的View時(shí),白色的四個(gè)角顯得很難看,可以根據(jù)需求設(shè)置。

    <!--明亮風(fēng)格的Dialog-->
    <style name="LightDialog" parent="Base.Theme.AppCompat.Light.Dialog">

    </style>
    <!--暗風(fēng)格的Dialog-->
    <style name="Dialog" parent="Base.Theme.AppCompat.Dialog">

    </style>
    <!--沒有背景的Dialog-->
    <style name="NoBackgroundDialog" parent="Base.Theme.AppCompat.Light.Dialog">
        <!--解決布局無法占據(jù)全屏的問題,是window的背景,比background高一級(jí)-->
        <item name="android:windowBackground">@null</item>
        <!--布局的背景-->
        <item name="android:background">@null</item>
        <!--window的前景-->
        <item name="android:windowFrame">@null</item>
        <!--昏暗效果啟用:false,則dialog的背景沒有半透明效果-->
        <item name="android:backgroundDimEnabled">true</item>
        <!--window是否半透明:好像沒有什么效果-->
        <item name="android:windowIsTranslucent">true</item>
        <!--懸浮于activity之上-->
        <item name="android:windowIsFloating">true</item>
    </style>

1.普通Dialog
private void bt1Click() {
        new AlertDialog.Builder(this, R.style.LightDialog)
                .setTitle("普通Dialog")
                .setMessage("消息內(nèi)容")
                .setNeutralButton("關(guān)閉", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        L.e("Dialog%d", which);
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        L.e("Dialog%d", which);
                    }
                })
                .setPositiveButton("確定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        L.e("Dialog%d", which);
                    }
                }).show();
    }
2.多選框
    private void bt2Click() {
        new AlertDialog.Builder(this, R.style.LightDialog)
                .setTitle("多選框")
                .setMultiChoiceItems(R.array.items, null, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        L.e("第%d個(gè)選擇%b", which, isChecked);
                    }
                }).show();
    }

3.單選框
 private void bt3Click() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.LightDialog);
        builder.setTitle("單選框");
        //方法1
        builder.setSingleChoiceItems(R.array.items, 1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                L.e(which);
            }
        });
        //方法2:類似于列表
        builder.setSingleChoiceItems(
                new ArrayAdapter<>(this, R.layout.item_dialog, R.id.tv_item_dialog, new String[]{"a", "b", "c"}),
                1,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        L.e(which);
                    }
                });
        builder.show();
    }
4.列表框
 private void bt4Click() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.LightDialog);
        builder.setTitle("列表框");
        //方法1
      /*  builder.setItems(R.array.items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                L.e(which);
            }
        });*/
        //方法2
        builder.setAdapter(
                new ArrayAdapter<>(this, R.layout.item_dialog, R.id.tv_item_dialog, new String[]{"a", "b", "c"}),
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        L.e(which);
                    }
                });
        builder.show();
    }

5.等待框
    private void bt5Click() {
        ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setTitle("等待框");
        progressDialog.setMessage("等待中");
        progressDialog.show();
    }
6.進(jìn)度框
    private void bt6Click() {
        ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setTitle("進(jìn)度條");
        progressDialog.setMessage("等待中");
        progressDialog.setIndeterminate(false);
        progressDialog.setMax(100);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.show();
        progressDialog.setProgress(60);
    }
7.自定義View框
 private void bt7Click() {

        AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.LightDialog);
        builder.setTitle("自定義View Dialog");
        AlertDialog alertDialog = null;
        View view = LayoutInflater.from(this).inflate(R.layout.view_dialog, null);
        builder.setView(view);
        alertDialog = builder.show();
        final AlertDialog finalAlertDialog = alertDialog;
        view.findViewById(R.id.tv_determine)
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finalAlertDialog.dismiss();
                    }
                });
        view.findViewById(R.id.tv_cancel)
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finalAlertDialog.dismiss();
                    }
                });
    }
8.自定義Dialog

    private void bt8Click() {
        CustomizeDialog customizeDialog = new CustomizeDialog(this);
        customizeDialog.setOnButtonClickListener(new CustomizeDialog.OnButtonClickListener() {
            @Override
            public void onCancelClick(Dialog dialog) {
                dialog.dismiss();
                Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onDetermineClick(Dialog dialog) {
                dialog.dismiss();
                Toast.makeText(MainActivity.this, "確定", Toast.LENGTH_SHORT).show();
            }
        });
        customizeDialog.show();
    }
9.封裝DialogUtils

    private void bt9Click() {
        View view = LayoutInflater.from(this).inflate(R.layout.view_dialog, null);
        final Dialog dialog = new DialogUtils()
                .with(this, R.style.NoBackgroundDialog)
                .contentView(view)
                .gravity(Gravity.BOTTOM)
                .width((int) (getResources().getDisplayMetrics().widthPixels * 0.9))
                .show();
        TextView tvCancel = view.findViewById(R.id.tv_cancel);
        TextView tvDetermine = view.findViewById(R.id.tv_determine);
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        tvDetermine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    }

DialogUtils:對(duì)于需要自定義View的Dialog,一些常用的操作可以簡(jiǎn)單封裝起來


import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.support.annotation.LayoutRes;
import android.view.View;


public class DialogUtils {

    private Params p;

    public DialogUtils() {
        p = new Params();
    }

    public DialogUtils with(Activity context, int theme) {
        p.context = context;
        p.mTheme = theme;
        return this;
    }

    public DialogUtils contentView(@LayoutRes int layoutResId) {
        p.mLayoutResId = layoutResId;
        return this;
    }

    public DialogUtils contentView(View view) {
        p.mContentView = view;
        return this;
    }

    public DialogUtils gravity(int gravity) {
        p.mGravity = gravity;
        return this;
    }

    public DialogUtils width(int widthPixels) {
        p.mWidth = widthPixels;
        return this;
    }

    public DialogUtils cancelable(boolean cancelable) {
        p.mCancelable = cancelable;
        return this;
    }

    public DialogUtils canceledOnTouchOutside(boolean canceledOnTouchOutside) {
        p.mCanceledOnTouchOutside = canceledOnTouchOutside;
        return this;
    }

    public Dialog create() {
        return apply(p);
    }

    public Dialog show() {
        Dialog dialog = this.create();
        dialog.show();
        return dialog;
    }

    private Dialog apply(Params p) {
        Dialog dialog = new Dialog(p.context, p.mTheme);
        if (p.mLayoutResId == 0 && p.mContentView != null) {
            dialog.setContentView(p.mContentView);
        }
        if (p.mLayoutResId != 0 && p.mContentView == null) {
            dialog.setContentView(p.mLayoutResId);
        }
        if (p.mLayoutResId != 0 && p.mContentView != null) {
            dialog.setContentView(p.mContentView);
        }
        if (p.mWidth != -1) {
            dialog.getWindow().getAttributes().width = p.mWidth;
        }
        if (p.mGravity != -1) {
            dialog.getWindow().getAttributes().gravity = p.mGravity;
        }
        dialog.setCancelable(p.mCancelable);
        dialog.setCanceledOnTouchOutside(p.mCanceledOnTouchOutside);
      
        return dialog;
    }

    public class Params {
        Context context;
        int mTheme;
        int mLayoutResId;
        View mContentView;//優(yōu)先使用contentView
        int mGravity = -1;
        int mWidth = -1;
        boolean mCancelable = true;
        boolean mCanceledOnTouchOutside = true;
    }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 前言 谷歌發(fā)布了 Material Design 設(shè)計(jì)之后,很多 Material 風(fēng)格的控件也隨之加入到了 V7...
    希靈丶閱讀 75,147評(píng)論 16 85
  • 概述 Dialog是Android的提示框,雖然說是很基礎(chǔ)的東西,但是里面其實(shí)還有很多值得學(xué)習(xí)交流的地方。本文將從...
    肖邦kaka閱讀 4,689評(píng)論 0 6
  • AlertDialog 使用 這篇文章將介紹一些 AlertDialog 常用的樣式,修改按鈕字體顏色,添加dia...
    numqin閱讀 1,290評(píng)論 0 3
  • Dialog 是android 原生的彈出框一共有7種吧(尾部有原生dialog的github地址) 首先如果要看...
    阿貍清純的容顏閱讀 386評(píng)論 0 0
  • ¥開啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個(gè)線程,因...
    小菜c閱讀 7,295評(píng)論 0 17

友情鏈接更多精彩內(nèi)容