46.Android 自定义Dialog

  • Android 自定义Dialog
    • 前言
    • 提示Dialog
    • 提示Dialog 效果图
    • 菜单Dialog
    • 菜单Dialog 效果图
    • DialogActivity

前言

提供两套自定义Dialog模板

  • 第一种,提示Dialog,有消失时间。

  • 第二种,菜单Dialog,用于用户交互。

提示Dialog

CustomDialog

public class CustomDialog extends Dialog {    private TextView dialogTV;    private static final long DEFAULT_DURATION = 1000L;    private static final String DEFAULT_CONTENT = "";    private long duration;    private String content;    private DialogCallback callback;    public CustomDialog(Context context) {        super(context, R.style.custom_dialog);        this.initViews(context);    }    /** * Creates a dialog window that uses a custom dialog style. * <p/> * The supplied {@code context} is used to obtain the window manager and * base theme used to present the dialog. * <p/> * The supplied {@code theme} is applied on top of the context's theme. See * <a href="{@docRoot}guide/topics/resources/available-resources.html#stylesandthemes"> * Style and Theme Resources</a> for more information about defining and * using styles. * * @param context the context in which the dialog should run * @param themeResId a style resource describing the theme to use for the * window, or {@code 0} to use the default dialog theme */    public CustomDialog(Context context, int themeResId) {        super(context, themeResId);        this.initViews(context);    }    public CustomDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {        super(context, cancelable, cancelListener);        this.initViews(context);    }    private void initViews(Context context) {        this.setContentView(R.layout.dialog_custom);        this.dialogTV = (TextView) this.findViewById(R.id.custom_dialog_tv);    }    @Override    public void show() {        super.show();        this.dialogTV.setText(TextUtils.isEmpty(this.content) ? DEFAULT_CONTENT : this.content);        long showDuration = this.duration > 0L ? this.duration : DEFAULT_DURATION;        new Handler().postDelayed(new Runnable() {            public void run() {                if (CustomDialog.this.isShowing()) {                    CustomDialog.this.dismiss();                    if (CustomDialog.this.callback != null) CustomDialog.this.callback.onDismiss();                }            }        }, showDuration);    }    public void setTextDrawable(Drawable drawable) {        if (drawable == null) return;        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());        this.dialogTV.setCompoundDrawables(drawable, null, null, null);    }    public interface DialogCallback {        void onDismiss();    }    public static class DialogBuilder {        private static String contextHashCode;        private static CustomDialog dialog;        public static DialogBuilder ourInstance;        public static DialogBuilder getInstance(Context context) {            if (ourInstance == null) ourInstance = new DialogBuilder();            String hashCode = String.valueOf(context.hashCode());            /** * 不同一个Activity */            if (!hashCode.equals(String.valueOf(contextHashCode))) {                contextHashCode = hashCode;                dialog = new CustomDialog(context);            }            return ourInstance;        }        public DialogBuilder setDuration(long duration) {            dialog.duration = duration;            return this;        }        public DialogBuilder setContent(String content) {            dialog.content = content;            return this;        }        public DialogBuilder setDrawable(Drawable drawable) {            dialog.setTextDrawable(drawable);            return this;        }        public DialogBuilder setCallback(DialogCallback callback) {            dialog.callback = callback;            return this;        }        public DialogBuilder setCanceledOnTouchOutside(boolean cancel) {            dialog.setCanceledOnTouchOutside(cancel);            return this;        }        public CustomDialog getDialog() {            return dialog;        }    }}

dialog_custom.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:gravity="center">    <TextView  android:id="@+id/custom_dialog_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/bg_dialog_custom_tv" android:drawableLeft="@mipmap/dialog_custom_tv_drawable" android:drawablePadding="5dp" android:drawableStart="@mipmap/dialog_custom_tv_drawable" android:gravity="center" android:text="成功" android:textColor="#ff666666" android:textSize="15sp" /></LinearLayout>

R.style.custom_dialog

<style name="custom_dialog" parent="@android:style/Theme.Dialog">    <item name="android:windowNoTitle">true</item>    <item name="android:windowBackground">@color/transparent</item>    <item name="android:windowIsTranslucent">false</item>    <item name="android:windowFullscreen">true</item>    <item name="android:windowIsFloating">true</item></style>

提示Dialog 效果图

菜单Dialog

MenuDialog

public class MenuDialog extends Dialog {    private TextView caseTV;    private TextView helpTV;    public MenuDialog(Context context) {        super(context, R.style.menu_dialog);        this.initViews(context);    }    /** * Creates a dialog window that uses a custom dialog style. * <p/> * The supplied {@code context} is used to obtain the window manager and * base theme used to present the dialog. * <p/> * The supplied {@code theme} is applied on top of the context's theme. See * <a href="{@docRoot}guide/topics/resources/available-resources.html#stylesandthemes"> * Style and Theme Resources</a> for more information about defining and * using styles. * * @param context the context in which the dialog should run * @param themeResId a style resource describing the theme to use for the * window, or {@code 0} to use the default dialog theme */    public MenuDialog(Context context, int themeResId) {        super(context, themeResId);        this.initViews(context);    }    public MenuDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {        super(context, cancelable, cancelListener);        this.initViews(context);    }    private void initViews(Context context) {        this.setContentView(R.layout.dialog_menu);        this.caseTV = (TextView) this.findViewById(R.id.dialog_menu_case_tv);        this.helpTV = (TextView) this.findViewById(R.id.dialog_menu_help_tv);    }    public void setTextDrawable(Drawable drawable) {        if (drawable == null) return;        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());    }    public static class DialogBuilder {        private static String contextHashCode;        private static MenuDialog dialog;        public static DialogBuilder ourInstance;        public static DialogBuilder getInstance(Context context) {            if (ourInstance == null) ourInstance = new DialogBuilder();            String hashCode = String.valueOf(context.hashCode());            /** * 不同一个Activity */            if (!hashCode.equals(String.valueOf(contextHashCode))) {                contextHashCode = hashCode;                dialog = new MenuDialog(context);            }            return ourInstance;        }        public DialogBuilder setCaseListenser(View.OnClickListener listener) {            dialog.caseTV.setOnClickListener(listener);            return this;        }        public DialogBuilder setHelpListener(View.OnClickListener listener) {            dialog.helpTV.setOnClickListener(listener);            return this;        }        public DialogBuilder setCanceledOnTouchOutside(boolean cancel) {            dialog.setCanceledOnTouchOutside(cancel);            return this;        }        public MenuDialog getDialog() {            return dialog;        }    }}

dialog_menu.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:layout_gravity="center" android:gravity="center" android:orientation="horizontal">    <TextView  android:id="@+id/dialog_menu_case_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/bg_dialog_menu_item" android:drawablePadding="13dp" android:drawableTop="@mipmap/dialog_menu_case" android:paddingBottom="13.5dp" android:paddingEnd="36dp" android:paddingLeft="36dp" android:paddingRight="36dp" android:paddingTop="20dp" android:gravity="center_horizontal" android:text="Case" android:textColor="#ff666666" android:textSize="14sp" />    <TextView  android:id="@+id/dialog_menu_help_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="16dp" android:background="@drawable/bg_dialog_menu_item" android:drawablePadding="13dp" android:drawableTop="@mipmap/dialog_menu_help" android:gravity="center_horizontal" android:paddingBottom="13.5dp" android:paddingEnd="36dp" android:paddingLeft="36dp" android:paddingRight="36dp" android:paddingTop="20dp" android:text="Help" android:textColor="#ff666666" android:textSize="14sp" /></LinearLayout>

R.style.menu_dialog

<style name="menu_dialog" parent="@android:style/Theme.Dialog">    <item name="android:windowNoTitle">true</item>    <item name="android:windowBackground">@color/transparent</item>    <item name="android:windowIsTranslucent">false</item>    <item name="android:windowFullscreen">true</item>    <item name="android:windowIsFloating">true</item></style>

菜单Dialog 效果图

DialogActivity

public class DialogActivity extends AppCompatActivity implements View.OnClickListener {    private MenuDialog menuDialog;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        this.setContentView(R.layout.activity_dialog);        this.menuDialog = MenuDialog.DialogBuilder.getInstance(this)                .setCaseListenser(new View.OnClickListener() {                    @Override                    public void onClick(View v) {                        Toast.makeText(DialogActivity.this, "case", Toast.LENGTH_SHORT).show();                        DialogActivity.this.menuDialog.dismiss();                    }                })                .setHelpListener(new View.OnClickListener() {                    @Override                    public void onClick(View v) {                        Toast.makeText(DialogActivity.this, "Help", Toast.LENGTH_SHORT).show();                        DialogActivity.this.menuDialog.dismiss();                    }                })                .getDialog();        this.initListeners();    }    private void initListeners() {        this.findViewById(R.id.dialog_custom).setOnClickListener(this);        this.findViewById(R.id.dialog_menu).setOnClickListener(this);    }    /** * Called when a view has been clicked. * * @param v The view that was clicked. */    @Override    public void onClick(View v) {        switch (v.getId()) {            case R.id.dialog_custom:                CustomDialog.DialogBuilder.getInstance(this)                        .setDuration(2000L)                        .setContent("CustomDialog")                        .setCanceledOnTouchOutside(false)                        .setCallback(new CustomDialog.DialogCallback() {                            @Override                            public void onDismiss() {                                Toast.makeText(DialogActivity.this, "CustomDialog dismiss", Toast.LENGTH_SHORT).show();                            }                        })                        .getDialog()                        .show();                break;            case R.id.dialog_menu:                this.menuDialog.show();                break;        }    }}

activity_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" android:padding="26dp">    <TextView  android:id="@+id/dialog_custom" style="@style/TextButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="CustomDialog" />    <TextView  android:id="@+id/dialog_menu" style="@style/TextButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="MenuDialog" /></LinearLayout>

更多相关文章

  1. Android(安卓)Studio 自动代码提示设置
  2. Android(安卓)Menu开源项目整合工程
  3. android api 中文 (74)―― AdapterView.AdapterContextMenuInfo
  4. android横向翻页滚动菜单
  5. toolbar自定义右边的菜单注意
  6. Android(安卓)消息数字提示,类似微信,BadgeView
  7. eclipse插件下载--全
  8. 属于自己的常见Android选项菜单样式集合
  9. android启动中遇到的2个问题

随机推荐

  1. 如何统计全天各个时间段产品销量情况(sqls
  2. 大数据量高并发的数据库优化详解
  3. SQL Server安装完成后3个需要立即修改的
  4. MySql查询不区分大小写解决方案(两种)
  5. 数据库设计三大范式简析
  6. 跨数据库实现数据交流
  7. 防御SQL注入的方法总结
  8. 用户 jb51net 登录失败。原因: 该帐户的
  9. 聚焦 Android(安卓)11: Google Play 应用
  10. Android(安卓)屏幕适配:最全面的解决方案