Dialog总结

本文详细对比了AlertDialog与Dialog的区别及使用场景,强调了谷歌推荐使用AlertDialog。特别介绍了AlertDialog.Builder类的使用方法,包括如何创建和展示对话框。此外,还提供了自定义DialogView的两种实现方式。

AlertDialog.Builder  AlertDialog   Dialog辨析

Dialog implements DialogInterface, Window.Callback, KeyEvent.Callback, OnCreateContextMenuListener, window.OnWindowDismissedCallback
AlertDialog extends Dialog implements DialogInterface {
     public static class Builder {}
}
一,谷歌建议使用之类 AlertDialog   ,不建议直接使用Dialog 。
二, AlertDialog.Builder 用来创建 AlertDialog  , 并简化设置参数的工具类,可直接用连续的"."符号设置参数。
如:
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        TextView tv = new TextView(builder.getContext());
        tv.setText("haha");
        builder.setTitle("aaa")
                .setView(tv)
                .show();
------------------------

AlertDialog.Builder 的api

   
  1. /**
  2. * Creates an {@link AlertDialog} with the arguments supplied to this
  3. * builder.
  4. * <p>
  5. * Calling this method does not display the dialog. If no additional
  6. * processing is needed, {@link #show()} may be called instead to both
  7. * create and display the dialog.
  8. */
  9. public AlertDialog create() {
  10. // Context has already been wrapped with the appropriate theme.
  11. final AlertDialog dialog = new AlertDialog(P.mContext, 0, false);
  12. P.apply(dialog.mAlert);
  13. dialog.setCancelable(P.mCancelable);
  14. if (P.mCancelable) {
  15. dialog.setCanceledOnTouchOutside(true);
  16. }
  17. dialog.setOnCancelListener(P.mOnCancelListener);
  18. dialog.setOnDismissListener(P.mOnDismissListener);
  19. if (P.mOnKeyListener != null) {
  20. dialog.setOnKeyListener(P.mOnKeyListener);
  21. }
  22. return dialog;
  23. }
---

   
  1. /**
  2. * Creates an {@link AlertDialog} with the arguments supplied to this
  3. * builder and immediately displays the dialog.
  4. * <p>
  5. * Calling this method is functionally identical to:
  6. * <pre>
  7. * AlertDialog dialog = builder.create();
  8. * dialog.show();
  9. * </pre>
  10. */
  11. public AlertDialog show() {
  12. final AlertDialog dialog = create();
  13. dialog.show();
  14. return dialog;
  15. }


半自定义Dialog View的两种方法

注:并非自定义view,而是利用Dialog本身提供的api进行自定义。
AlertDialog

   
  1. public static void showSimpleDialog(Context context, String msg, View.OnClickListener listener) {
  2. final AlertDialog.Builder builder = new AlertDialog.Builder(context);
  3. final AlertDialog dialog = builder.create();
  4. LayoutInflater inflater = LayoutInflater.from(context);
  5. View view = inflater.inflate(R.layout.dialog_simple_deposit, null);
  6. TextView tvConfirm = (TextView) view.findViewById(R.id.tv_confirm);
  7. tvConfirm.setOnClickListener(listener);
  8. tvConfirm.setOnClickListener(new View.OnClickListener() {
  9. @Override
  10. public void onClick(View v) {
  11. dialog.dismiss();
  12. }
  13. });
  14. TextView tvMsg = (TextView) view.findViewById(R.id.tv_msg);
  15. tvMsg.setText(msg);
  16. dialog.setView(view);
  17. dialog.setCancelable(false);// 返回键不可取消
  18. dialog.setView(view, 0, 0, 0, 0);// 可消除部分边框,当不能全部消除
  19. dialog.show();
  20. }
--------------------------------------
Dialog

   
  1. public static void showSimpleDialog1(Context context, String msg) {
  2. final Dialog dialog =new Dialog(context, R.style.dialog);// 可以消除黑色边框
  3. LayoutInflater inflater = LayoutInflater.from(context);
  4. View view = inflater.inflate(R.layout.dialog_simple_deposit, null);
  5. TextView tvConfirm = (TextView) view.findViewById(R.id.tv_confirm);
  6. // tvConfirm.setOnClickListener(listener);
  7. tvConfirm.setOnClickListener(new View.OnClickListener() {
  8. @Override
  9. public void onClick(View v) {
  10. dialog.dismiss();
  11. }
  12. });
  13. TextView tvMsg = (TextView) view.findViewById(R.id.tv_msg);
  14. tvMsg.setText(msg);
  15. dialog.setContentView(view);
  16. dialog.setCancelable(false);// 返回键不可取消
  17. dialog.show();
  18. }
---

   
  1. <style name="dialog" parent="@android:style/Theme.Dialog">
  2. <item name="android:windowFrame">@null</item>
  3. <item name="android:windowIsFloating">true</item>
  4. <item name="android:windowIsTranslucent">true</item>
  5. <item name="android:windowNoTitle">true</item>
  6. <item name="android:background">@android:color/transparent</item>
  7. <item name="android:windowBackground">@android:color/transparent</item>
  8. <item name="android:backgroundDimEnabled">true</item>
  9. <item name="android:backgroundDimAmount">0.6</item>
  10. </style>





转载于:https://www.cnblogs.com/chenchengzhi/p/5057835.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值