I hope that the EditTextPreference accept only a value between 6000 and 65536, how can I do? Thanks!
BTW, the code A doesn't work, I can't input any number in the edit box of EditTextPreference which key is "WebServerPort"
<EditTextPreference
android:key="WebServerPort"
android:defaultValue="6000"
android:title="Port"
android:summary="Set port for web server, it should be greater than 6000 and less than 65536"
android:inputType="number"
android:layout="@layout/layout_customize_preference_item"
/>
Code A
public class UIPreference extends PreferenceActivity {
private AdView adView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.mypreference);
setContentView(R.layout.layout_preference);
EditTextPreference edit_Pref = (EditTextPreference) getPreferenceScreen().findPreference("WebServerPort");
edit_Pref.getEditText().setFilters(new InputFilter[]{ new InputFilterMinMax("6000", "65536")});
}
}
class InputFilterMinMax implements InputFilter {
private int min, max;
public InputFilterMinMax(int min, int max) {
this.min = min;
this.max = max;
}
public InputFilterMinMax(String min, String max) {
this.min = Integer.parseInt(min);
this.max = Integer.parseInt(max);
}
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
try {
int input = Integer.parseInt(dest.toString() + source.toString());
if (isInRange(min, max, input))
return null;
} catch (NumberFormatException nfe) { }
return "";
}
private boolean isInRange(int a, int b, int c) {
return b > a ? c >= a && c <= b : c >= b && c <= a;
}
}