码迷,mamicode.com
首页 > 其他好文 > 详细

Dialog总结

时间:2015-12-18 20:27:30      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:

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>





Dialog总结

标签:

原文地址:http://www.cnblogs.com/chenchengzhi/p/5057835.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!