zhangzengfei
2022-01-10 4496b59ab27d569df1da7ef634e02273b3a9618a
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
package com.basic.security.widget;
 
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
 
import com.basic.security.R;
 
public class ConfirmDialog extends Dialog {
 
    private Context context;
    private String title;
    private String leftBtnText;
    private String rightBtnText;
    private ClickListenerInterface clickListenerInterface;
 
    public ConfirmDialog(Context context, String title, String leftBtnText, String rightBtnText) {
        super(context, R.style.ConfirmDialogStyle);
        this.context = context;
        this.title = title;
        this.leftBtnText = leftBtnText;
        this.rightBtnText = rightBtnText;
    }
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }
 
    public void init() {
 
        LayoutInflater inflater = LayoutInflater.from(context);
 
        View view = inflater.inflate(R.layout.confirm_dialog, null);
        setContentView(view);
 
        TextView tvTitle = view.findViewById(R.id.title);
        Button tvConfirm = view.findViewById(R.id.leftBtn);
        TextView tvCancel = view.findViewById(R.id.rightBit);
 
        tvTitle.setText(title);
        tvConfirm.setText(leftBtnText);
        tvCancel.setText(rightBtnText);
 
        tvConfirm.setOnClickListener(new clickListener());
        tvCancel.setOnClickListener(new clickListener());
 
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        lp.width = (int) (d.widthPixels * 0.8); // 高度设置为屏幕的0.6
        dialogWindow.setAttributes(lp);
        hideSystemUI();
        initSystemUIListener();
    }
 
    private void initSystemUIListener() {
        if (getWindow() != null) {
            getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
                @Override
                public void onSystemUiVisibilityChange(int visibility) {
                    hideSystemUI();
                }
            });
 
        }
    }
 
    private void hideSystemUI() {
        if (getWindow() != null) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                    | View.SYSTEM_UI_FLAG_IMMERSIVE);
        }
    }
 
    @Override
    public void show() {
 
        try {
            hideSystemUI();
            super.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
 
    }
 
    public void setClickListener(ClickListenerInterface clickListenerInterface) {
        this.clickListenerInterface = clickListenerInterface;
    }
 
    public interface ClickListenerInterface {
 
        void doConfirm();
 
        void doCancel();
    }
 
    private class clickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            int id = v.getId();
            switch (id) {
                case R.id.leftBtn:
                    clickListenerInterface.doConfirm();
                    break;
                case R.id.rightBit:
                    clickListenerInterface.doCancel();
                    break;
            }
        }
 
    }
 
    ;
}