android_自定義彈出框

代碼部分
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
public class CustomDialog extends Dialog {
    private View dialogContent;
    private TextView titleView;
 
    public View getDialogContent() {
        return dialogContent;
    }
 
    private CustomDialog(Context context, int theme) {
        super(context, theme);
    }
 
    public void setTitle(String title) {
        if (null != titleView)
            titleView.setText(title);
    }
 
    public static void showMessage(String message) {
        showMessage(message, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        });
    }
 
    public static void showMessage(String message, OnClickListener listener) {
        new CustomDialog.Builder(BaseActivity.currentActivity())
                .setTitle(Constant.STR_PROMPT)
                .setCancelable(truenull)
                .setMessage(message)
                .setPositiveButton(R.string.dialog_ok, listener).create().show();
    }
 
    public static class Builder {
        private Context context;
        private String title;
        private String message;
        private String positiveBtnText;
        private String negativeBtnText;
        private View contentView;
        boolean cancelable = false;
        OnCancelListener onCancelListener;
        OnDismissListener onDismissListener;
 
        private OnClickListener positiveBtnOnClickedListener;
        private OnClickListener negativeBtnOnClickedListener;
 
        public Builder(Context context) {
            this.context = context;
        }
 
        public Builder setCancelable(boolean cancelable, OnCancelListener listener) {
            this.cancelable = cancelable;
            this.onCancelListener = listener;
            return this;
        }
 
        public Builder setOnDismissListener(OnDismissListener onDismissListener) {
            this.onDismissListener = onDismissListener;
            return this;
        }
 
        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }
 
        public Builder setTitle(int titleId) {
            this.title = (String) context.getText(titleId);
            return this;
        }
 
        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }
 
        public Builder setMessage(int messageId) {
            this.message = (String) context.getText(messageId);
            return this;
        }
 
        public Builder setContentView(View contentView) {
            this.contentView = contentView;
            return this;
        }
 
        public Builder setContentView(int layoutId) {
            this.contentView = ((LayoutInflater) context.getSystemService(
                    Context.LAYOUT_INFLATER_SERVICE)).inflate(layoutId, null);
            return this;
        }
 
        public Builder setPositiveButton(String text, OnClickListener listener) {
            this.positiveBtnText = text;
            this.positiveBtnOnClickedListener = listener;
            return this;
        }
 
        public Builder setPositiveButton(int textId, OnClickListener listener) {
            this.positiveBtnText = (String) context.getText(textId);
            this.positiveBtnOnClickedListener = listener;
            return this;
        }
 
        public Builder setNegativeButton(String text, OnClickListener listener) {
            this.negativeBtnText = text;
            this.negativeBtnOnClickedListener = listener;
            return this;
        }
 
        public Builder setNegativeButton(int textId, OnClickListener listener) {
            this.negativeBtnText = (String) context.getText(textId);
            this.negativeBtnOnClickedListener = listener;
            return this;
        }
 
        public CustomDialog create() {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final CustomDialog dialog = new CustomDialog(context, R.style.CustomDialog);
            View dialogView = inflater.inflate(R.layout.custom_dialog, null);
            dialog.addContentView(dialogView, new LayoutParams(LayoutParams.MATCH_PARENT,
                    LayoutParams.WRAP_CONTENT));
            TextView titleTv = (TextView) dialogView.findViewById(R.id.custom_dialog_title);
            dialog.titleView = titleTv;
            TextView messageTv = (TextView) dialogView.findViewById(R.id.custom_dialog_content_text);
            Button positiveBtn = (Button) dialogView.findViewById(R.id.custom_dialog_positive_button);
            Button negativeBtn = (Button) dialogView.findViewById(R.id.custom_dialog_negative_button);
            LinearLayout contentLayout = (LinearLayout) dialogView.findViewById(R.id.custom_dialog_content);
 
            dialog.setCancelable(cancelable);
            if (cancelable && null != onCancelListener)
                dialog.setOnCancelListener(onCancelListener);
            if null != onDismissListener)
                dialog.setOnDismissListener(onDismissListener);
            // 標題
            if (!TextUtils.isEmpty(title)) {
                titleTv.setVisibility(View.VISIBLE);
                dialogView.findViewById(R.id.custom_dialog_top_line).setVisibility(View.VISIBLE);
                titleTv.setText(title);
            else {
                dialogView.findViewById(R.id.custom_dialog_top_line).setVisibility(View.INVISIBLE);
                titleTv.setVisibility(View.GONE);
            }
 
            // Message Or ContentView
            if (!TextUtils.isEmpty(message)) {
                messageTv.setText(message);
                dialog.dialogContent = messageTv;
            else if (contentView != null) {
                contentLayout.removeAllViews();
                contentLayout.addView(contentView, new LayoutParams(LayoutParams.MATCH_PARENT,
                        LayoutParams.WRAP_CONTENT));
                dialog.dialogContent = contentView;
            }
 
            // PositiveButton or NegativeButton
            if (!TextUtils.isEmpty(positiveBtnText) || !TextUtils.isEmpty(negativeBtnText)) { // 有按鈕
                dialogView.findViewById(R.id.custom_dialog_bottom_line).setVisibility(View.VISIBLE);
                if (!TextUtils.isEmpty(positiveBtnText)) {
                    positiveBtn.setVisibility(View.VISIBLE);
                    positiveBtn.setText(positiveBtnText);
                    if (positiveBtnOnClickedListener != null) {
                        positiveBtn.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                positiveBtnOnClickedListener.onClick(dialog, Dialog.BUTTON_POSITIVE);
                            }
                        });
                    }
                else {
                    positiveBtn.setVisibility(View.GONE);
                }
                if (!TextUtils.isEmpty(negativeBtnText)) {
                    negativeBtn.setVisibility(View.VISIBLE);
                    negativeBtn.setText(negativeBtnText);
                    if (negativeBtnOnClickedListener != null) {
                        negativeBtn.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                negativeBtnOnClickedListener.onClick(dialog, Dialog.BUTTON_NEGATIVE);
                            }
                        });
                    }
                else {
                    negativeBtn.setVisibility(View.GONE);
                }
                if (!(!TextUtils.isEmpty(positiveBtnText) && !(TextUtils.isEmpty(negativeBtnText)))) {
                    dialogView.findViewById(R.id.custom_dialog_button_line).setVisibility(View.GONE);
                else {
                    dialogView.findViewById(R.id.custom_dialog_button_line).setVisibility(View.VISIBLE);
                }
            else // 無按鈕
                dialogView.findViewById(R.id.custom_dialog_button_line).setVisibility(View.GONE);
                dialogView.findViewById(R.id.custom_dialog_bottom_line).setVisibility(View.INVISIBLE);
                dialogView.findViewById(R.id.custom_dialog_button_layout).setVisibility(View.GONE);
            }
            return dialog;
        }
 
    }
}
佈局文件
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   style="@style/CustomDialogBackground"
    android:minWidth="@dimen/min_width_dialog" >
 
    <TextView
        android:id="@+id/custom_dialog_title"
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:textColor="@android:color/black"
        android:textSize="@dimen/title_small"
        android:gravity="center" />
 
    <View
        android:id="@+id/custom_dialog_top_line"
        android:layout_width="match_parent"
        android:layout_height="@dimen/sigle_pexls"
        android:background="@color/gray" />
 
    <LinearLayout
        android:id="@+id/custom_dialog_content"
        android:layout_width="wrap_content"
        android:layout_height="0px"
        android:gravity="center"
        android:padding="@dimen/medium"
        android:minHeight="@dimen/min_height_dialog_content"
        android:layout_weight="1">
 
        <TextView
            android:id="@+id/custom_dialog_content_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:maxWidth="240dp"
            android:layout_gravity="center_vertical"
            android:textColor="@color/font_black"
            android:textSize="@dimen/text_noraml"/>
 
    </LinearLayout>
 
    <View
        android:id="@+id/custom_dialog_bottom_line"
        android:layout_width="match_parent"
        android:layout_height="@dimen/sigle_pexls"
        android:background="@color/gray" />
 
    <LinearLayout
        android:id="@+id/custom_dialog_button_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
 
        <Button
            android:id="@+id/custom_dialog_positive_button"
            style="@style/CustomDialogButton"/>
 
        <View
            android:id="@+id/custom_dialog_button_line"
            android:layout_width="@dimen/sigle_pexls"
            android:layout_height="match_parent"
            android:background="@color/gray"/>
 
        <Button
            android:id="@+id/custom_dialog_negative_button"
            style="@style/CustomDialogButton"/>
    </LinearLayout>
 
</LinearLayout>
樣式

<style name="CustomDialogBackground">

        <item name="android:layout_width">wrap_content</item>

        <item name="android:layout_height">wrap_content</item>

        <item name="android:background">@drawable/rectangle_write</item>

        <item name="android:gravity">center</item>

        <item name="android:orientation">vertical</item>

    </style>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章