Android首选项:如何验证其值?特别是EdiBoxPreference

时间:2012-11-16 20:36:11

标签: android sharedpreferences

我们如何验证输入到EditBoxPrefence中的值?

例如,用户输入类似

的目录名称

myfolder //或/ myfolder或

我想修剪任何斜线,然后存储首选项的值 怎么可能?

1 个答案:

答案 0 :(得分:1)

您可以测试该值并告诉用户出错的地方: 或做任何你想做的事情

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preferences);

    Your_Pref = (EditTextPreference) getPreferenceScreen().findPreference("Your_Pref");

    Your_Pref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {

        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            Boolean rtnval = true;
            if (Your_Test) {
                final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                builder.setTitle("Invalid Input");
                builder.setMessage("Something's gone wrong...");
                builder.setPositiveButton(android.R.string.ok, null);
                builder.show();
                rtnval = false;
            }
            return rtnval;
        }
    });
}