android自定义dialog

本文介绍了如何在Android开发中创建一个通用的自定义Dialog,通过提供代码示例和使用场景,帮助开发者快速复用和定制Dialog。文章包含Dialog的截图,EditDialog.java的代码以及edit_dialog_layout.xml布局文件的内容,并给出了使用该Dialog的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

开发中经常会用到dialog,各种各样的,觉得还是写个通用的自定义dialog,以后直接套用,在这里贴上代码,为了方便以后查阅。

先看效果图

这里写图片描述

EditDialog.java

public class EditDialog extends Dialog {

    public EditDialog(Context context) {
        super(context);
    }

    public EditDialog(Context context, int theme) {
        super(context, theme);
    }

    public static class Builder {
        private Context context;
        private String title;


        public Builder setEditText(String text) {
            editText = text;
            return this;
        }

        public EditText getmEditTextView() {
            return mEditTextView;
        }

        private EditText mEditTextView;
        private String editText;
        private String positiveButtonText;
        private String negativeButtonText;
        private View contentView;
        private DialogInterface.OnClickListener positiveButtonClickListener;
        private DialogInterface.OnClickListener negativeButtonClickListener;

        public Builder(Context context) {
            this.context = context;
        }


        /**
         * Set the Dialog title from resource
         *
         * @param title
         * @return
         */
        public Builder setTitle(int title) {
            this.title = (String) context.getText(title);
            return this;
        }

        /**
         * Set the Dialog title from String
         *
         * @param title
         * @return
         */

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setContentView(View v) {
            this.contentView = v;
            return this;
        }

        /**
         * Set the positive button resource and it's listener
         *
         * @param positiveButtonText
         * @return
         */
        public Builder setPositiveButton(int positiveButtonText,
                                         DialogInterface.OnClickListener listener) {
            this.positiveButtonText = (String) context
                    .getText(positiveButtonText);
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setPositiveButton(String positiveButtonText,
                                         DialogInterface.OnClickListener listener) {
            this.positiveButtonText = positiveButtonText;
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(int negativeButtonText,
                                         DialogInterface.OnClickListener listener) {
            this.negativeButtonText = (String) context
                    .getText(negativeButtonText);
            this.negativeButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(String negativeButtonText,
                                         DialogInterface.OnClickListener listener) {
            this.negativeButtonText = negativeButtonText;
            this.negativeButtonClickListener = listener;
            return this;
        }

        public EditDialog create() {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // instantiate the dialog with the custom Theme
            final EditDialog dialog = new EditDialog(context,R.style.edit_dialog);
            View layout = inflater.inflate(R.layout.edit_dialog_layout, null);
            dialog.addContentView(layout, new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            // set the dialog title
            ((TextView) layout.findViewById(R.id.title)).setText(title);
            mEditTextView = (EditText) layout.findViewById(R.id.editText);
            ((EditText) layout.findViewById(R.id.editText)).setText(editText);
            // set the confirm button
            if (positiveButtonText != null) {
                ((Button) layout.findViewById(R.id.positiveButton))
                        .setText(positiveButtonText);
                if (positiveButtonClickListener != null) {
                    ((Button) layout.findViewById(R.id.positiveButton))
                            .setOnClickListener(new View.OnClickListener() {
                                public void onClick(View v) {
                                    positiveButtonClickListener.onClick(dialog,
                                            DialogInterface.BUTTON_POSITIVE);
                                }
                            });
                }
            } else {
                // if no confirm button just set the visibility to GONE
                layout.findViewById(R.id.positiveButton).setVisibility(
                        View.GONE);
            }
            // set the cancel button
            if (negativeButtonText != null) {
                ((Button) layout.findViewById(R.id.negativeButton))
                        .setText(negativeButtonText);
                if (negativeButtonClickListener != null) {
                    ((Button) layout.findViewById(R.id.negativeButton))
                            .setOnClickListener(new View.OnClickListener() {
                                public void onClick(View v) {
                                    negativeButtonClickListener.onClick(dialog,
                                            DialogInterface.BUTTON_NEGATIVE);
                                }
                            });
                }
            } else {
                // if no confirm button just set the visibility to GONE
                layout.findViewById(R.id.negativeButton).setVisibility(
                        View.GONE);
            }
            // set the content message
            if (contentView != null) {
                // if no message set
                // add the contentView to the dialog body
                ((LinearLayout) layout.findViewById(R.id.content))
                        .removeAllViews();
                ((LinearLayout) layout.findViewById(R.id.content))
                        .addView(contentView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
            }
            dialog.setContentView(layout);
            return dialog;
        }
    }
}

edit_dialog_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="@drawable/edit_dialog_bg"
    android:orientation="vertical"
    android:minWidth="300dp"
    >

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_gravity="center_horizontal"
        android:text="@string/edit_guide"
        android:textColor="@color/public_text_black"
        android:textSize="15sp" />

    <LinearLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="@dimen/dp_70"
        android:layout_margin="@dimen/dp_10"
        android:background="@drawable/edit_bg">


        <EditText
            android:id="@+id/editText"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="5dp"
            android:background="@null"
            android:textColor="@color/public_text_black"
            android:textSize="13sp"
            android:gravity="top"
            android:textCursorDrawable="@null"/>
    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/dp_60"
        android:layout_gravity="bottom"
        android:gravity="center"
        android:orientation="horizontal">

        <Button
            android:id="@+id/negativeButton"
            android:layout_width="0dp"
            android:layout_height="@dimen/dp_40"
            android:gravity="center"
            android:layout_weight="1"
            android:layout_marginLeft="@dimen/dp_10"
            android:layout_marginRight="@dimen/dp_10"
            android:text="@string/cancel"
            android:textColor="@color/public_text_gray"
            android:textSize="@dimen/size_15"
            android:background="@drawable/edit_button_bg"/>

        <Button
            android:id="@+id/positiveButton"
            android:layout_width="0dp"
            android:layout_height="@dimen/dp_40"
            android:layout_marginLeft="@dimen/dp_10"
            android:layout_marginRight="@dimen/dp_10"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/ok"
            android:textColor="@color/public_orange"
            android:textSize="@dimen/size_15"
            android:background="@drawable/edit_button_bg"/>


    </LinearLayout>
</LinearLayout>

最后贴上使用这个dialog的地方

       final EditDialog.Builder builder = new EditDialog.Builder(this);
        builder.setTitle("编辑导语");
        builder.setEditText("helloword");
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
                String edit = builder.getmEditTextView().getText().toString();
                Log.i("shenlong","edit="+edit);
            }
        });

        builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        });


        builder.create().show();

如果自己实现出来效果不好,应该是有些东西没有设置好,这里是源码地址

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值