Android开发之自定义对话框(2)—加入EditText

由于项目需要,在弹出的对话框中要有编辑框,先看效果图:
Android开发之自定义对话框(2)—加入EditText_第1张图片

1,先看布局文件:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="240dp"
    android:layout_height="wrap_content"
    android:background="@drawable/dialog_bkg_2"
    android:orientation="vertical">


    <TextView
        android:id="@+id/dialog_title"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="@drawable/dialog_title_bkg"
        android:gravity="center"
        android:paddingBottom="4dp"
        android:paddingTop="4dp"
        android:textColor="@color/white"
        android:text="@string/add_friend"
        android:textSize="20sp" />


        <EditText
            android:id="@+id/edt_dialog_message"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:padding="4dp"
            android:hint="请输入新好友用户名"
            android:background="@drawable/dialog_bkg_edit"
            android:textColor="@drawable/radio_btn_text"
            android:textSize="18sp" />


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="35dp"
        android:layout_marginBottom="16dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_dialog_ok"
            style="@style/btn_style"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:layout_weight="1"
            android:text="@string/button_add"
            android:background="@drawable/button_selector"
            android:textColor="@color/white"
            android:textAllCaps="false"
            android:textSize="18sp" />

        <Button
            android:id="@+id/btn_dialog_cancel"
            style="@style/btn_style"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:layout_weight="1"
            android:text="@string/button_cancel"
            android:background="@drawable/button_selector"
            android:textColor="@color/white"
            android:textAllCaps="false"
            android:textSize="18sp" />

    LinearLayout>


LinearLayout>

2,自定义dialog:

/**
 * Created by gyq on 2017/5/5 08:39
 */
public class CustomDialog extends Dialog {
    public CustomDialog(Context context) {
        super(context);
    }

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

    public static class Builder {
        private Context context;
        private String title;
        private String message;
        private float textSize;
        private String positiveButtonText;
        private String negativeButtonText;
        private View contentView;
        private EditText firendName;
        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;
        }

        private Builder setMessage(String msg) {
            this.message = msg;
            return this;
        }

        public Builder setMessage(String message, float textSize) {
            this.message = message;
            this.textSize = textSize;
            return this;
        }

        public Builder setMessage(int message) {
            this.message = (String) context.getText(message);
            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;
        }

        public String getFriendName() {
            return firendName.getText().toString().trim();
        }

        /**
         * 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 CustomDialog create() {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // instantiate the dialog with the custom Theme
            final CustomDialog dialog = new CustomDialog(context,R.style.Dialog);
            dialog.setCanceledOnTouchOutside(false);
            dialog.setCancelable(false);

            View layout = inflater.inflate(R.layout.dialog_clear_normal, null);
            dialog.addContentView(layout, new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            // set the dialog title
            ((TextView) layout.findViewById(R.id.dialog_title)).setText(title);

            firendName =  (EditText)layout.findViewById(R.id.edt_dialog_message);
            // set the confirm button
            if (positiveButtonText != null) {
                ((Button) layout.findViewById(R.id.btn_dialog_ok))
                        .setText(positiveButtonText);
                if (positiveButtonClickListener != null) {
                    ((Button) layout.findViewById(R.id.btn_dialog_ok))
                            .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.btn_dialog_ok).setVisibility(
                        View.GONE);
            }
            // set the cancel button
            if (negativeButtonText != null) {
                ((Button) layout.findViewById(R.id.btn_dialog_cancel))
                        .setText(negativeButtonText);
                if (negativeButtonClickListener != null) {
                    ((Button) layout.findViewById(R.id.btn_dialog_cancel))
                            .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.btn_dialog_cancel).setVisibility(
                        View.GONE);
            }
            // set the content message
            if (message != null) {
                TextView msgView = (TextView) layout.findViewById(R.id.message);
                msgView.setText(message);
                if (textSize != 0) {
                    msgView.setTextSize(textSize);
                }
            } else if (contentView != null) {

            }
            dialog.setContentView(layout);
            return dialog;
        }

    }
}

3,如何使用:

private void addFirend() {
        final CustomDialog.Builder builder = new CustomDialog.Builder(getActivity());
        builder.setTitle("添加好友");
        builder.setPositiveButton("添加", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(final DialogInterface dialog, int which) {
                new Thread() {
                    @Override
                    public void run() {
                        String friendName = builder.getFriendName();
                        try{
                            EMClient.getInstance().contactManager().addContact(friendName,"我是你的朋友");
                        }catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                }.start();

                dialog.dismiss();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.create().show();
    }

你可能感兴趣的:(Android开发之自定义对话框(2)—加入EditText)