Android自定义输入框

public class InputCustomDialog extends Dialog {
    public InputCustomDialog(Context context, int theme) {
        super(context, theme);
    }

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

    /**
     * Helper class for creating a custom dialog
     */
    public static class Builder {

        private Context context;
        private String title;
        private String message;
        private View contentView;
        private int inputType;
        private EditText editDialogMessage;
        private DialogInterface.OnClickListener positiveButtonClickListener;
        private DialogInterface.OnClickListener negativeButtonClickListener;

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

        /**
         * Set the Dialog message from String
         *
         * @param
         * @return
         */
        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

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

        public String getMessage(){
            return message;
        }

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

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

        /**
         * Set a custom content view for the Dialog. If a message is set, the
         * contentView is not added to the Dialog...
         *
         * @param v
         * @return
         */
        public Builder setContentView(View v) {
            this.contentView = v;
            return this;
        }

        public String getEditDialogMessage(){
            return editDialogMessage.getText().toString().trim();
        }

        public Builder setInputType(int type){
            this.inputType  = type;
            return this;
        }

        /**
         * Set the positive button text and it's listener
         *
         * @param listener
         * @return
         */
        public Builder setPositiveButton( DialogInterface.OnClickListener listener) {
            this.positiveButtonClickListener = listener;
            return this;
        }

        /**
         * Set the negative button text and it's listener
         *
         * @param listener
         * @return
         */
        public Builder setNegativeButton(DialogInterface.OnClickListener listener) {
            this.negativeButtonClickListener = listener;
            return this;
        }

        /**
         * Create the custom dialog
         */
        public InputCustomDialog create() {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            // instantiate the dialog with the custom Theme
            final InputCustomDialog dialog = new InputCustomDialog(context, R.style.Dialog);
            dialog.setCanceledOnTouchOutside(false);

            View layout = inflater.inflate(R.layout.layout_input_custom_dialog, null);
            dialog.addContentView(layout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            // set the dialog title
            if (title != null) {
                ((TextView) layout.findViewById(R.id.edit_dialog_title)).setText(title);
            }
            editDialogMessage = (EditText)layout.findViewById(R.id.edit_dialog_message);
            editDialogMessage.setInputType(inputType);
            // set the confirm button
            if (positiveButtonClickListener != null) {
                ((Button) layout.findViewById(R.id.edit_dialog_positive_button))
                        .setOnClickListener(new View.OnClickListener() {
                            public void onClick(View v) {
                                positiveButtonClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
                            }
                        });
            }
            // set the cancel button
            if (negativeButtonClickListener != null) {
                ((ImageView) layout.findViewById(R.id.edit_dialog_negative_button))
                        .setOnClickListener(new View.OnClickListener() {
                            public void onClick(View v) {
                                negativeButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
                            }
                        });
            }
            dialog.setContentView(layout);
            return dialog;
        }

    }
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="345dp"
    android:layout_height="200dp"
    android:background="@drawable/dialog_background"
    android:orientation="vertical" >
    
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/edit_dialog_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#666666"
            android:textSize="25sp"
            android:layout_centerInParent="true" />
        <ImageView
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:src="@drawable/cancel"
            android:layout_alignParentRight="true"
            android:layout_marginRight="20dp"
            android:padding="7dp"
            android:id="@+id/edit_dialog_negative_button"/>
    </RelativeLayout>

    <EditText
        android:layout_width="295dp"
        android:layout_height="45dp"
        android:textSize="20sp"
        android:textColor="#666666"
        android:id="@+id/edit_dialog_message"
        android:background="@drawable/custom_edit_view"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"/>
    <Button
        android:layout_width="187dp"
        android:layout_height="73dp"
        android:background="@drawable/confirm"
        android:id="@+id/edit_dialog_positive_button"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"/>

</LinearLayout>


猜你喜欢

转载自blog.csdn.net/u014611408/article/details/80899034