如何验证在Android 2.1中输入的EditTextPreference的格式和值?

时间:2021-09-19 00:30:20

Does anyone have sample code to validate user entered text in preferences? For example, I have a EditTextPreference for user to enter an email address. I'd like to validate the format of email address entered and pop up an alert dialog if the format isn't correct. Anyone have any sample code for this? Thanks

有没有人有示例代码来验证用户在首选项中输入的文字?例如,我有一个EditTextPreference供用户输入电子邮件地址。我想验证输入的电子邮件地址的格式,如果格式不正确,则弹出警告对话框。有人有任何示例代码吗?谢谢

3 个解决方案

#1


9  

Your question was an early google hit when I was trying to do the same thing, so hopefully this helps someone. Here's something I hacked together today that demonstrates OnPreferenceChangeListener, thus allowing you to stop invalid changes.

当我尝试做同样的事情时,你的问题是早期的谷歌热门,所以希望这有助于某人。这是今天我一起入侵的东西,它演示了OnPreferenceChangeListener,从而允许你停止无效的更改。

in your fragment:

在你的片段中:

    @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;
            }
        });
    }

#2


9  

Implement Preference.OnPreferenceChangeListener

实现Preference.OnPreferenceChangeListener

boolean onPreferenceChange(Preference preference, Object newValue)

boolean onPreferenceChange(首选项首选项,对象newValue)

Called when a Preference has been changed by the user. This is called before the state of the Preference is about to be updated and before the state is persisted.

当用户更改了首选项时调用。在即将更新Preference状态之前和状态持久之前调用此方法。

Returns True to update the state of the Preference with the new value.

返回True以使用新值更新“首选项”的状态。

So you can directly return the result of value validation.

所以你可以直接返回值验证的结果。

public class Preferences extends PreferenceActivity implements OnSharedPreferenceChangeListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
        findPreference("mail_preference_key").setOnPreferenceChangeListener(
            new Preference.OnPreferenceChangeListener() {

            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                return Pattern.matches("mailPattern", (String) newValue);
            }

        });
    }
}

#3


2  

I'd use Preference.OnPreferenceChangeListener rather than SharedPreferences.OnSharedPreferenceChangeListener.

我使用Preference.OnPreferenceChangeListener而不是SharedPreferences.OnSharedPreferenceChangeListener。

The former allows you to validate the new value before it's persisted (and prevent it from being persisted) rather than after.

前者允许您在持久化之前验证新值(并防止它被持久化),而不是之后。

#1


9  

Your question was an early google hit when I was trying to do the same thing, so hopefully this helps someone. Here's something I hacked together today that demonstrates OnPreferenceChangeListener, thus allowing you to stop invalid changes.

当我尝试做同样的事情时,你的问题是早期的谷歌热门,所以希望这有助于某人。这是今天我一起入侵的东西,它演示了OnPreferenceChangeListener,从而允许你停止无效的更改。

in your fragment:

在你的片段中:

    @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;
            }
        });
    }

#2


9  

Implement Preference.OnPreferenceChangeListener

实现Preference.OnPreferenceChangeListener

boolean onPreferenceChange(Preference preference, Object newValue)

boolean onPreferenceChange(首选项首选项,对象newValue)

Called when a Preference has been changed by the user. This is called before the state of the Preference is about to be updated and before the state is persisted.

当用户更改了首选项时调用。在即将更新Preference状态之前和状态持久之前调用此方法。

Returns True to update the state of the Preference with the new value.

返回True以使用新值更新“首选项”的状态。

So you can directly return the result of value validation.

所以你可以直接返回值验证的结果。

public class Preferences extends PreferenceActivity implements OnSharedPreferenceChangeListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
        findPreference("mail_preference_key").setOnPreferenceChangeListener(
            new Preference.OnPreferenceChangeListener() {

            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                return Pattern.matches("mailPattern", (String) newValue);
            }

        });
    }
}

#3


2  

I'd use Preference.OnPreferenceChangeListener rather than SharedPreferences.OnSharedPreferenceChangeListener.

我使用Preference.OnPreferenceChangeListener而不是SharedPreferences.OnSharedPreferenceChangeListener。

The former allows you to validate the new value before it's persisted (and prevent it from being persisted) rather than after.

前者允许您在持久化之前验证新值(并防止它被持久化),而不是之后。