如何validation特定int范围的EditTextPreference值

我想validationEditTextPreference值是否在特定的int范围内(比如说1到22)。 我可以validation的唯一部分是inputType (仅限数字),在prefs.xml文件中包含以下xml声明。

机器人:的inputType = “数量”

我做的第二件事是在ListSettings活动中编写以下代码。

 public class ListSettings extends PreferenceActivity implements OnSharedPreferenceChangeListener{ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.prefs); SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); sp.registerOnSharedPreferenceChangeListener(this); } public void onSharedPreferenceChanged(SharedPreferences sp, String key) { String value = sp.getString(key, null); int intZoomValue = Integer.parseInt(value); if (intZoomValue  22) { Intent intent = new Intent().setClass(this, dialog.class); // I want to call the dialog activity here... } } 

而dialog.class如下:

 public class dialog extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("Please enter a value between 1-22!"); builder.setCancelable(true); AlertDialog alert = builder.create(); alert.show(); } } 

我想要实现的是每当用户输入不在1-22范围内的值时调用dialog.class (警告对话框)。

我无法真正理解如何以正确的方式validation。

如果输入的值无效,将值限制在允许的范围内可能会更加用户友好,并通过Toast通知用户您已经这样做了。

如果您的范围很小,例如1-22,那么您甚至可以考虑使用Spinner而不是EditText这样用户就无法在第一时间输入无效值。

这就是我做的:

  EditTextPreference pref = (EditTextPreference) findPreference(this.getString(R.string.my_setting_key)); pref.getEditText().setFilters(new InputFilter[]{ new InputFilter (){ @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 (input0) return null; } catch (NumberFormatException nfe) { } return ""; } } }); 

所以诀窍是你可以使用以下命令从EditTextPreference获取EditText:

  pref.getEditText() 

然后你可以设置一个filter。 当一切正常时,filter应返回null,而当用户输入的char将使文本无效时,filter应返回空字符串。

另见这里

你可以使用setPositivteButton并在用户点击Okt时validation输入,如果还不行,请重新启动对话框。

InputFilter解决方案不起作用,尤其是使用selectAllOnFocus。 如果您不能使用建议的选择器/微调器替代方案,例如,当您要强制执行最小值而不是最大值时,可以在值无效时禁用“确定”按钮,如下所示:

 public static void setNumberRange(final EditTextPreference preference, final Number min, final Number max) { setNumberRange(preference, min, max, false); } public static void setNumberRange(final EditTextPreference preference, final Number min, final Number max, final boolean allowEmpty) { preference.getEditText().addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void afterTextChanged(final Editable s) { Dialog dialog = preference.getDialog(); if (dialog instanceof AlertDialog) { Button button = ((AlertDialog) dialog).getButton(DialogInterface.BUTTON_POSITIVE); if (allowEmpty && s.length() == 0) { button.setEnabled(true); return; } try { if (min instanceof Integer || max instanceof Integer) { int input = Integer.parseInt(s.toString()); button.setEnabled((min == null || input >= min.intValue()) && (max == null || input <= max.intValue())); } else if (min instanceof Float || max instanceof Float) { float input = Float.parseFloat(s.toString()); button.setEnabled((min == null || input >= min.floatValue()) && (max == null || input <= max.floatValue())); } else if (min instanceof Double || max instanceof Double) { double input = Double.parseDouble(s.toString()); button.setEnabled((min == null || input >= min.doubleValue()) && (max == null || input <= max.doubleValue())); } } catch (Exception e) { button.setEnabled(false); } } } }); } 

这允许您将最小值和/或最大值指定为Integer,Float或Double(无限制传递null)。 您可以选择允许/禁止空值。 (最初接受的值无效。)