a
554325746@qq.com
2020-01-13 7171bbcdb2859ea93f3af69d817243752b08314a
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package com.basic.security.utils;
 
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
 
import com.mcivicm.editablespinner.EditableSpinner;
 
import java.text.SimpleDateFormat;
import java.util.Date;
 
import static com.basic.security.utils.ActivityUtil.mainActivity;
 
public class AlterDialogUtils extends Dialog {
    private Context context;
    private String title;
    private String leftBtnText;
    private String rightBtnText;
    private AlterDialogUtils.ClickListenerInterface clickListenerInterface;
 
    static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    public TextView valid_guest_time;
    public EditableSpinner spinner_d, spinner_h;
 
    public AlterDialogUtils(Context context, String title, String leftBtnText, String rightBtnText) {
        super(context, com.basic.security.utils.RUtils.R_style_AlterDialogStyle);
        this.context = context;
        this.title = title;
        this.leftBtnText = leftBtnText;
        this.rightBtnText = rightBtnText;
    }
 
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }
 
    public void init() {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(com.basic.security.utils.ResolutionAdaptation.selecttime_dialog(), null);
        setContentView(view);
        initViewStatus(view);
//        TextView tvTitle = view.findViewById(com.basic.security.utils.RUtils.R_id_title);
        Button tvConfirm = view.findViewById(com.basic.security.utils.RUtils.R_id_leftBtn);
        TextView tvCancel = view.findViewById(com.basic.security.utils.RUtils.R_id_rightBit);
//        TextView valid_guest_time = view.findViewById(com.basic.security.utils.RUtils.R_id_valid_guest_time);
//        tvTitle.setText(title);
        tvConfirm.setText(leftBtnText);
        tvCancel.setText(rightBtnText);
//        valid_guest_time.setText(sdf.format(new Date()));
        tvConfirm.setOnClickListener(new AlterDialogUtils.clickListener());
        tvCancel.setOnClickListener(new AlterDialogUtils.clickListener());
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        if (Constants.landscape) {
            lp.width = (int) (d.widthPixels * 0.4); // 宽度设置为屏幕的0.4
        } else {
            lp.width = (int) (d.widthPixels * 0.8); // 高度设置为屏幕的0.6
        }
        dialogWindow.setAttributes(lp);
        SystemUI.hideSystemUI(this);
        SystemUI.initSystemUIListener(this);
    }
 
    public void show() {
        try {
            this.getWindow().getDecorView().setVisibility(View.INVISIBLE);
            SystemUI.hideSystemUI(this);
            SystemUI.initSystemUIListener(this);
            super.show();
            this.getWindow().getDecorView().setVisibility(View.VISIBLE);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    public void setClickListener(AlterDialogUtils.ClickListenerInterface clickListenerInterface) {
        this.clickListenerInterface = clickListenerInterface;
    }
 
    public interface ClickListenerInterface {
        void doConfirm();
 
        void doCancel();
    }
 
    public void initViewStatus(View view){
 
        valid_guest_time = view.findViewById(com.basic.security.utils.RUtils.R_id_valid_guest_time);
        valid_guest_time.setText(sdf.format(new Date()));
        Log.v("default-iniView","初始化view:"+ valid_guest_time.getText());
        final TextView add_h = view.findViewById(com.basic.security.utils.RUtils.R_id_add_h);
        final TextView reduce_h = view.findViewById(com.basic.security.utils.RUtils.R_id_reduce_h);
        final TextView add_d = view.findViewById(com.basic.security.utils.RUtils.R_id_add_d);
        final TextView reduce_d = view.findViewById(com.basic.security.utils.RUtils.R_id_reduce_d);
 
        add_h.setOnClickListener(new View.OnClickListener(){
 
            @Override
            public void onClick(View v) {
                double d = Float.parseFloat(spinner_h.getText().toString()) + 0.5;
                spinner_h.setText((Double.toString(d)));
                addHalfHouse("hour","add");
            }
        });
        reduce_h.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                double d = Float.parseFloat(spinner_h.getText().toString()) - 0.5;
                spinner_h.setText((Double.toString(d)));
                addHalfHouse("hour","reduce");
            }
        });
        add_d.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                double d = Float.parseFloat(spinner_d.getText().toString()) + 1;
                spinner_h.setText((Double.toString(d)));
                addHalfHouse("day","add");
            }
        });
        reduce_d.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                double d = Float.parseFloat(spinner_d.getText().toString()) - 1;
                spinner_h.setText((Double.toString(d)));
                addHalfHouse("day","reduce");
            }
        });
 
        spinner_d = view.findViewById(com.basic.security.utils.RUtils.R_id_editable_spinner_d);
 
        final String[] items = new String[]{"1", "2", "3", "4"};
        spinner_d.setDropDownStrings(items);
        spinner_d.setText("0");
        spinner_d.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(mainActivity(), items[position], Toast.LENGTH_LONG).show();
                addHalfHouse("spinner","other");
            }
        });
        spinner_d.setOnAfterTextChanged(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                Log.v("default-beforeChanged","开始输入");
            }
 
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Log.v("default-onChanged","文字变化");
            }
 
            @Override
            public void afterTextChanged(Editable s) {
                Log.v("default-afterChange","输入结束");
                addHalfHouse("spinner","other");
            }
        });
 
        spinner_h = view.findViewById(com.basic.security.utils.RUtils.R_id_editable_spinner_h);
 
        final String[] items_h = new String[]{"0.5", "1", "1.5", "2"};
        spinner_h.setDropDownStrings(items_h);
        spinner_h.setText("1");
        addHalfHouse("spinner","other");
        spinner_h.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(mainActivity(), items_h[position], Toast.LENGTH_LONG).show();
                addHalfHouse("spinner","other");
            }
        });
        spinner_h.setOnAfterTextChanged(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                Log.v("default-beforeChanged","开始输入");
            }
 
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Log.v("default-onChanged","文字变化");
            }
 
            @Override
            public void afterTextChanged(Editable s) {
                Log.v("default-afterChange","输入结束");
                addHalfHouse("spinner","other");
            }
        });
    }
 
    public void addHalfHouse(String dayOrhour,String type){
        try {
            Editable days = spinner_d.getText();
            Editable hourse = spinner_h.getText();
            Date currentTime = new Date();
 
            float df = 0;
            float hf = 0;
 
            if(!TextUtils.isEmpty(days)){
                df = Float.parseFloat(days.toString());
            }
            if(!TextUtils.isEmpty(hourse)){
                hf = Float.parseFloat(hourse.toString());
            }
            double ts =(df) * (24*3600*1000) + (hf) * (60*60*1000);
            if(dayOrhour.equalsIgnoreCase("day")){
                if(type.equalsIgnoreCase("add")){
                    ts = (df + 1) * (24*3600*1000) + (hf) * (60*60*1000);
                }else{
                    ts = (df - 1) * (24*3600*1000) + (hf) * (60*60*1000);
                }
            }
            if(dayOrhour.equalsIgnoreCase("hour")){
                if(type.equalsIgnoreCase("add")){
                    ts = (df) * (24*3600*1000) + (hf + 0.5) * (60*60*1000);
                }else{
                    ts = (df) * (24*3600*1000) + (hf  - 0.5) * (60*60*1000);
                }
            }
 
            long resultTime =  (long) (currentTime.getTime() + ts);
 
            Log.v("default-addHourse","days:"+ sdf.format(resultTime));
 
            valid_guest_time.setText(sdf.format(resultTime));
 
            Log.v("default-addHourse","days:"+ days +" hourse:"+ hourse + " current:" + currentTime.getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    private class clickListener implements View.OnClickListener {
        public void onClick(View v) {
            int id = v.getId();
            switch (id) {
                case com.basic.security.utils.RUtils.R_id_leftBtn:
                    clickListenerInterface.doConfirm();
                    break;
                case com.basic.security.utils.RUtils.R_id_rightBit:
                    clickListenerInterface.doCancel();
                    break;
            }
        }
    }
 
}