-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMoneySpinner.java
executable file
·169 lines (138 loc) · 5.25 KB
/
MoneySpinner.java
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
package com.min60.flowers.ui.uni;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.text.InputType;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.afollestad.materialdialogs.MaterialDialog;
import com.min60.flowers.R;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;
public class MoneySpinner extends LinearLayout {
private float initialValue = 0;
private float maxValue = Float.MAX_VALUE;
private float minValue = Float.MIN_VALUE;
private float value;
private float increment = 1;
private MaterialDialog dialog;
private boolean isManualChanged = false;
DecimalFormat df = new DecimalFormat("€#.##");
TextView tvValue;
OnValueChangedListener listener;
private void closeDialog() {
if (dialog != null && dialog.isShowing()) {
dialog.dismiss();
dialog = null;
}
}
public void setListener(OnValueChangedListener listener) {
this.listener = listener;
}
public MoneySpinner(Context context) {
super(context);
init(null, 0);
}
public MoneySpinner(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs, 0);
}
public MoneySpinner(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(attrs, defStyle);
}
private void init(AttributeSet attrs, int defStyle) {
// Load attributes
df.setMinimumFractionDigits(2);
df.setMinimumIntegerDigits(1);
df.setMaximumFractionDigits(2);
df.setDecimalFormatSymbols(new DecimalFormatSymbols(getLocale(getContext())));
final TypedArray taParams = getContext().obtainStyledAttributes(
attrs, R.styleable.MoneySpinner, defStyle, 0);
minValue = taParams.getFloat(R.styleable.MoneySpinner_minFloatValue, minValue);
initialValue = taParams.getFloat(R.styleable.MoneySpinner_initialFloatValue, 1);
value = initialValue;
increment = taParams.getFloat(R.styleable.MoneySpinner_incremental, 1);
maxValue = taParams.getFloat(R.styleable.MoneySpinner_maxFloatValue, maxValue);
taParams.recycle();
inflate(getContext(), R.layout.number_spinner, this);
tvValue = findViewById(R.id.tvValue);
tvValue.setEnabled(isEnabled());
tvValue.setOnClickListener(view -> {
dialog = new MaterialDialog.Builder(getContext())
.title(getContext().getString(R.string.money_amount))
.inputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL)
.input(getContext().getString(R.string.single_price), "" + value, false,
(dialog1, input) -> {
})
.positiveText(R.string._save)
.negativeText(R.string._cancel)
.onPositive((dialog12, which) -> {
try {
isManualChanged = true;
setValue(Float.parseFloat(dialog12.getInputEditText().getText().toString()));
} catch (Exception e) {
e.printStackTrace();
}
})
.show();
});
findViewById(R.id.btnPlus).setOnClickListener(view -> {
if (value <= maxValue - increment) {
isManualChanged = true;
value += increment;
showValue();
}
});
findViewById(R.id.btnPlus).setEnabled(isEnabled());
findViewById(R.id.btnMinus).setOnClickListener(view -> {
if (value >= minValue + increment) {
isManualChanged = true;
value -= increment;
showValue();
}
});
findViewById(R.id.btnMinus).setEnabled(isEnabled());
showValue();
}
public boolean isManualChanged() {
return isManualChanged;
}
@Override
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
findViewById(R.id.btnMinus).setVisibility(enabled ? View.VISIBLE : INVISIBLE);
findViewById(R.id.btnPlus).setVisibility(enabled ? View.VISIBLE : INVISIBLE);
tvValue.setEnabled(enabled);
}
@Override
protected void finalize() throws Throwable {
super.finalize();
closeDialog();
}
private void showValue() {
tvValue.setText(df.format(value));
if (listener != null) listener.onChange(value);
}
public float getValue() {
return value;
}
public void setValue(float value) {
this.value = value;
showValue();
}
public interface OnValueChangedListener {
void onChange(float value);
}
private Locale getLocale(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return context.getResources().getConfiguration().getLocales().get(0);
} else {
//noinspection deprecation
return context.getResources().getConfiguration().locale;
}
}
}