1

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

例如,用户输入一个目录名称,如

我的文件夹// 或 /myfolder 或

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

4

1 回答 1

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;
        }
    });
}
于 2012-11-16T20:45:53.677 回答