1
1
package cc .calliope .mini .dialog ;
2
2
3
3
import android .content .Context ;
4
+ import android .graphics .Color ;
4
5
import android .graphics .drawable .ColorDrawable ;
6
+ import android .util .Pair ;
5
7
import android .view .LayoutInflater ;
6
8
import android .view .View ;
7
9
import android .view .Window ;
12
14
import androidx .appcompat .app .AlertDialog ;
13
15
import cc .calliope .mini .R ;
14
16
15
- public class DialogUtils {
17
+ public final class DialogUtils {
18
+ private DialogUtils () {
19
+ }
20
+
21
+ private static Pair <AlertDialog , View > createDialog (Context context , int layoutResId ) {
22
+ View view = LayoutInflater .from (context ).inflate (layoutResId , null );
23
+ AlertDialog alertDialog = new AlertDialog .Builder (context )
24
+ .setView (view )
25
+ .create ();
26
+ setTransparentBackground (alertDialog );
27
+ return new Pair <>(alertDialog , view );
28
+ }
29
+
30
+ private static void setTransparentBackground (AlertDialog dialog ) {
31
+ Window window = dialog .getWindow ();
32
+ if (window != null ) {
33
+ window .setBackgroundDrawable (new ColorDrawable (Color .TRANSPARENT ));
34
+ }
35
+ }
16
36
17
37
public static void showEditDialog (Context context , String title , String input , OnEditDialogListener listener ) {
18
- View view = LayoutInflater . from (context ). inflate ( R .layout .dialog_edit , null );
19
- AlertDialog . Builder builder = new AlertDialog . Builder ( context ) ;
20
- builder . setView ( view ) ;
38
+ Pair < AlertDialog , View > pair = createDialog (context , R .layout .dialog_edit );
39
+ AlertDialog alertDialog = pair . first ;
40
+ View view = pair . second ;
21
41
22
42
TextView dialogTitle = view .findViewById (R .id .dialog_title );
23
43
EditText dialogInput = view .findViewById (R .id .dialog_input );
@@ -27,16 +47,9 @@ public static void showEditDialog(Context context, String title, String input, O
27
47
dialogTitle .setText (title );
28
48
dialogInput .setText (input );
29
49
30
- AlertDialog alertDialog = builder .create ();
31
- Window window = alertDialog .getWindow ();
32
- if (window != null ) {
33
- window .setBackgroundDrawable (new ColorDrawable (0 ));
34
- }
35
-
36
50
dialogOkButton .setOnClickListener (v -> {
37
- String inputText = dialogInput .getText ().toString ();
38
51
if (listener != null ) {
39
- listener .onOkButtonClicked (inputText );
52
+ listener .onOkButtonClicked (dialogInput . getText (). toString () );
40
53
}
41
54
alertDialog .dismiss ();
42
55
});
@@ -45,10 +58,27 @@ public static void showEditDialog(Context context, String title, String input, O
45
58
alertDialog .show ();
46
59
}
47
60
61
+ public static void showInfoDialog (Context context , String title , String message ) {
62
+ Pair <AlertDialog , View > pair = createDialog (context , R .layout .dialog_info );
63
+ AlertDialog alertDialog = pair .first ;
64
+ View view = pair .second ;
65
+
66
+ TextView dialogTitle = view .findViewById (R .id .dialog_title );
67
+ TextView dialogMessage = view .findViewById (R .id .dialog_message );
68
+ Button dialogCancelButton = view .findViewById (R .id .dialog_cancel );
69
+
70
+ dialogTitle .setText (title );
71
+ dialogMessage .setText (message );
72
+
73
+ dialogCancelButton .setOnClickListener (v -> alertDialog .dismiss ());
74
+
75
+ alertDialog .show ();
76
+ }
77
+
48
78
public static void showWarningDialog (Context context , String title , String message , OnWarningDialogListener listener ) {
49
- AlertDialog . Builder builder = new AlertDialog . Builder (context );
50
- View view = LayoutInflater . from ( context ). inflate ( R . layout . dialog_warning , null ) ;
51
- builder . setView ( view ) ;
79
+ Pair < AlertDialog , View > pair = createDialog (context , R . layout . dialog_warning );
80
+ AlertDialog alertDialog = pair . first ;
81
+ View view = pair . second ;
52
82
53
83
TextView dialogTitle = view .findViewById (R .id .dialog_title );
54
84
TextView dialogMessage = view .findViewById (R .id .dialog_message );
@@ -58,12 +88,6 @@ public static void showWarningDialog(Context context, String title, String messa
58
88
dialogTitle .setText (title );
59
89
dialogMessage .setText (message );
60
90
61
- AlertDialog alertDialog = builder .create ();
62
- Window window = alertDialog .getWindow ();
63
- if (window != null ) {
64
- window .setBackgroundDrawable (new ColorDrawable (0 ));
65
- }
66
-
67
91
dialogOkButton .setOnClickListener (v -> {
68
92
if (listener != null ) {
69
93
listener .onOkButtonClicked ();
@@ -72,7 +96,6 @@ public static void showWarningDialog(Context context, String title, String messa
72
96
});
73
97
74
98
dialogCancelButton .setOnClickListener (v -> alertDialog .dismiss ());
75
-
76
99
alertDialog .show ();
77
100
}
78
101
0 commit comments