Domanda

I am implementing a Preference where users enter and save a PIN number. I am using an EditTextPreference for that and the PIN number should exactly be 4 digits. In order to validate it, I used the following OnPreferenceChangeListener() method

final EditTextPreference pinRetrieve = (EditTextPreference)getPreferenceScreen().findPreference("create_pin");

pinRetrieve.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object o) {
                Boolean rtnval = true;
                if (TextUtils.isEmpty(pinRetrieve.getText())){
                    final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                    builder.setTitle(R.string.pin_error_heading);
                    builder.setMessage(R.string.pin_alert_pin_not_set);
                    builder.setPositiveButton(android.R.string.ok, null);
                    builder.show();
                    pinSetting.setChecked(false);
                    rtnval = false;

                } else if (pinRetrieve.getText().length() < 4){
                    final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                    builder.setTitle(R.string.pin_error_heading);
                    builder.setMessage(R.string.pin_alert_pin_less_digits);
                    builder.setPositiveButton(android.R.string.ok, null);
                    builder.show();
                    pinSetting.setChecked(false);
                    rtnval = false;
                }
                return rtnval;
            }
        });

Even when I enter 4 digits, I am getting the alert box with R.string.pin_alert_pin_not_set as the message, what I think happening is it always goes to if (TextUtils.isEmpty(pinRetrieve.getText())), even when it is not empty.

Can't I use TextUtils.isEmpty(pinRetrieve.getText()) on EditBoxPreference? If I can why does it always return true even when the pinRetrieve.getText() is not empty?

Thank you

È stato utile?

Soluzione

The second parameter passed to onPreferenceChange(Preference preference, Object o) is the new Value of the preference you can check that instead of using the EditTextPreference reference. You will also be able to drop the final keyword. And get rid of the warnings. All you need is cast the o value to a String. If your preferences are added from an xml file you can add an android:maxLength="4" attribute - this will not allow the user to type in more than 4 characters in the EditText.

Altri suggerimenti

The isEmpty function of TextUtils class accepts a char sequence as an argument and the getText of EditTextPreferences returns a string.

Rather try

(TextUtils.isEmpty(new CharSequence(pinRetrieve.getText())))

String implements the CharSequence interface.

You can try to use the subsequence function of the string class and it will return a charsequence.

String pin = pinRetrievce.getText();
CharSequnce pinSequence = pin.subSequence(0, pin.length());
(TextUtils.isEmpty(pinSequence)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top