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
|
package de.danoeh.antennapod.preferences;
import android.app.AlertDialog;
import android.content.Context;
import android.support.v7.preference.Preference;
import android.text.InputFilter;
import android.util.AttributeSet;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import de.danoeh.antennapod.R;
public class NumberPickerPreference extends Preference {
private Context context;
private int defaultValue = 0;
private int minValue = 0;
private int maxValue = Integer.MAX_VALUE;
public NumberPickerPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context, attrs);
}
public NumberPickerPreference(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
public NumberPickerPreference(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public NumberPickerPreference(Context context) {
super(context);
this.context = context;
}
private void init(Context context, AttributeSet attrs) {
this.context = context;
for (int i = 0; i < attrs.getAttributeCount(); i++) {
String name = attrs.getAttributeName(i);
String value = attrs.getAttributeValue(i);
switch (name) {
case "defaultValue":
defaultValue = Integer.parseInt(value);
break;
case "minValue":
minValue = Integer.parseInt(value);
break;
case "maxValue":
maxValue = Integer.parseInt(value);
break;
}
}
}
@Override
protected void onClick() {
super.onClick();
View view = View.inflate(context, R.layout.numberpicker, null);
EditText number = view.findViewById(R.id.number);
number.setText(getSharedPreferences().getString(getKey(), ""+defaultValue));
number.setFilters(new InputFilter[]{(source, start, end, dest, dstart, dend) -> {
try {
String newVal = dest.toString().substring(0, dstart) + dest.toString().substring(dend);
newVal = newVal.substring(0, dstart) + source.toString() + newVal.substring(dstart);
int input = Integer.parseInt(newVal);
if (input >= minValue && input <= maxValue) {
return null;
}
} catch (NumberFormatException nfe) {
nfe.printStackTrace();
}
return "";
}});
AlertDialog dialog = new AlertDialog.Builder(context)
.setTitle(getTitle())
.setView(view)
.setNegativeButton(android.R.string.cancel, null)
.setPositiveButton(android.R.string.ok, (dialogInterface, i) -> {
try {
String numberString = number.getText().toString();
int value = Integer.parseInt(numberString);
if (value < minValue || value > maxValue) {
return;
}
getSharedPreferences().edit().putString(getKey(), "" + value).apply();
if (getOnPreferenceChangeListener() != null) {
getOnPreferenceChangeListener().onPreferenceChange(this, value);
}
} catch (NumberFormatException e) {
// Do not set value
}
})
.create();
dialog.show();
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}
|