termux-app

Форк
0
99 строк · 4.2 Кб
1
package com.termux.shared.interact;
2

3
import android.app.Activity;
4
import android.app.AlertDialog;
5
import android.content.Context;
6
import android.content.DialogInterface;
7
import android.graphics.Color;
8
import android.view.LayoutInflater;
9
import android.view.View;
10
import android.widget.Button;
11
import android.widget.TextView;
12

13
import com.termux.shared.R;
14
import com.termux.shared.logger.Logger;
15

16
public class MessageDialogUtils {
17

18
    /**
19
     * Show a message in a dialog
20
     *
21
     * @param context The {@link Context} to use to start the dialog. An {@link Activity} {@link Context}
22
     *                must be passed, otherwise exceptions will be thrown.
23
     * @param titleText The title text of the dialog.
24
     * @param messageText The message text of the dialog.
25
     * @param onDismiss The {@link DialogInterface.OnDismissListener} to run when dialog is dismissed.
26
     */
27
    public static void showMessage(Context context, String titleText, String messageText, final DialogInterface.OnDismissListener onDismiss) {
28
        showMessage(context, titleText, messageText, null, null, null, null, onDismiss);
29
    }
30

31
    /**
32
     * Show a message in a dialog
33
     *
34
     * @param context The {@link Context} to use to start the dialog. An {@link Activity} {@link Context}
35
     *                must be passed, otherwise exceptions will be thrown.
36
     * @param titleText The title text of the dialog.
37
     * @param messageText The message text of the dialog.
38
     * @param positiveText The positive button text of the dialog.
39
     * @param onPositiveButton The {@link DialogInterface.OnClickListener} to run when positive button
40
     *                         is pressed.
41
     * @param negativeText The negative button text of the dialog. If this is {@code null}, then
42
     *                         negative button will not be shown.
43
     * @param onNegativeButton The {@link DialogInterface.OnClickListener} to run when negative button
44
     *                         is pressed.
45
     * @param onDismiss The {@link DialogInterface.OnDismissListener} to run when dialog is dismissed.
46
     */
47
    public static void showMessage(Context context, String titleText, String messageText,
48
                                   String positiveText,
49
                                   final DialogInterface.OnClickListener onPositiveButton,
50
                                   String negativeText,
51
                                   final DialogInterface.OnClickListener onNegativeButton,
52
                                   final DialogInterface.OnDismissListener onDismiss) {
53

54
        AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.Theme_AppCompat_Light_Dialog);
55

56
        LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
57
        View view = inflater.inflate(R.layout.dialog_show_message, null);
58
        if (view != null) {
59
            builder.setView(view);
60

61
            TextView titleView = view.findViewById(R.id.dialog_title);
62
            if (titleView != null)
63
                titleView.setText(titleText);
64

65
            TextView messageView = view.findViewById(R.id.dialog_message);
66
            if (messageView != null)
67
                messageView.setText(messageText);
68
        }
69

70
        if (positiveText == null)
71
            positiveText = context.getString(android.R.string.ok);
72
        builder.setPositiveButton(positiveText, onPositiveButton);
73

74
        if (negativeText != null)
75
            builder.setNegativeButton(negativeText, onNegativeButton);
76

77
        if (onDismiss != null)
78
            builder.setOnDismissListener(onDismiss);
79

80
        AlertDialog dialog = builder.create();
81

82
        dialog.setOnShowListener(dialogInterface -> {
83
            Logger.logError("dialog");
84
            Button button = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
85
            if (button != null)
86
                button.setTextColor(Color.BLACK);
87
            button = dialog.getButton(AlertDialog.BUTTON_NEGATIVE);
88
            if (button != null)
89
                button.setTextColor(Color.BLACK);
90
        });
91

92
        dialog.show();
93
    }
94

95
    public static void exitAppWithErrorMessage(Context context, String titleText, String messageText) {
96
        showMessage(context, titleText, messageText, dialog -> System.exit(0));
97
    }
98

99
}
100

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.