简体   繁体   中英

EditTextPreference inputType=textPassword not working

I've created a SettingsActivity with the template and put an EditTextPreference in my root_preferences.xml. It should contain a password, so I edited it like this:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/colorBackground">

<EditTextPreference
        android:id="@+id/etPassword"
        android:dialogTitle="Passwort"
        android:inputType="textPassword"
        android:key="pref_password"
        android:selectAllOnFocus="true"
        android:singleLine="true"
        android:title="Passwort" />

My problem is that neither inputType, singleLine nor setAllOnFocus is working. Do you know what's the problem?

In my case, InputType doesn't hide the password on input and also in the summary. Here's how I got around the problem

XML file

<PreferenceScreen
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <EditTextPreference
        app:key="pref_password"
        app:title="Password"
        app:dialogTitle="Set password"
        app:useSimpleSummaryProvider="true"
    />
</PreferenceScreen>

In the PreferenceFragmentCompat set in your XML, find your EditTextPreference in the onCreatePreferences section and add an OnBindEditTextListener on it.

public static class YourFragment extends PreferenceFragmentCompat {
  @Override
  public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
    // Find the password EditText
    EditTextPreference etpPassword = getPreferenceManager().findPreference("pref_password");

    etpPassword.setOnBindEditTextListener(new EditTextPreference.OnBindEditTextListener() {
      @Override
      public void onBindEditText(@NonNull EditText editText) {
        // Set keyboard layout and some behaviours of the field
        editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);

        // Replace -> android:singleLine="true"
        // Not needed for password field, or set it before setTransformationMethod
        // otherwise the password will not be hidden
        //editText.setSingleLine(true);

        // Replace -> android:inputType="textPassword"
        // For hiding text
        editText.setTransformationMethod(PasswordTransformationMethod.getInstance());

        // Replace -> android:selectAllOnFocus="true"
        // On password field, you cannot make a partial selection with .setSelection(start, stop)
        editText.selectAll();

        // Replace -> android:maxLength="99"
        editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(99)});
      }
    });          
  }
}

You also can create your own EditTextPreference class and set other things.

public class YourEditTextPreference extends EditTextPreference {

  // Add some preferences, which can be used later for checking
  private Integer mPasswordMinSize = 6;

  public EditTextPreferencePassword(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    init();
  }

  public EditTextPreferencePassword(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
  }

  public EditTextPreferencePassword(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
  }

  public EditTextPreferencePassword(Context context) {
    super(context);
    init();
  }

  private void init(){
    // Set Dialog button text
    this.setNegativeButtonText("RETURN");
    this.setPositiveButtonText("CHECK");

    this.setOnBindEditTextListener(new OnBindEditTextListener() {
      @Override
      public void onBindEditText(@NonNull EditText editText) {
        // Put field parameters here
      }
    });
  }

  public void setMinSize(int minSize) { mPasswordMinSize = minSize; }
  public Integer getMinSize(){ return mPasswordMinSize; }

  // Hide password by stars
  @Override
  public CharSequence getSummary() {
    return getText().equals("") ? super.getSummary() : "*******";
  }
}

And in your XML, change <EditTextPreference to <complet.path.to.YourEditTextPreference

As pointed out in a previous answer, inputType is not understood by EditTextPreference and it seems it's not able to pass it to the underlying EditText object by itself, as one would expect.

Adding to the problem, there seems to be a lack of consistency depending on which libraries you are using. The accepted answer doesn't seem to work with AndroidX because there's no such getEditText() method in that EditTextPreference implementation. There's a way, though, by adding a listener that's exactly there for that (Kotlin example):

    val myPref = findPreference<EditTextPreference>(
        "my_pref_key"
    )

    myPref?.setOnBindEditTextListener {
        it.inputType = InputType.TYPE_CLASS_NUMBER
    }

Finally does the trick, but I have wasted a good hour trying to figure out a solution for this, mostly reading outdated questions and answers on the matter.

You can't do it from XML, but EditTextpreference exposes the EditText so you can do it programmatically. After you load the preferences in your Activity/Fragment, you can do:

   EditTextPreference pref = (EditTextPreference) 
   PreferenceManager.findPreference("edit");  
   EditText prefEditText = pref.getEditText();
   prefEditText.setInputType(InputType.TYPE_CLASS_TEXT); // set properties here
   prefEditText.setSingleLine(true);

I see it's been awhile but setting this inside the SettingsFragment onViewCreated() worked for me...

val pw = preferenceScreen.preferenceManager.findPreference<EditTextPreference>("password")
pw?.setOnBindEditTextListener {
    it.inputType = InputType.TYPE_CLASS_TEXT + InputType.TYPE_TEXT_VARIATION_PASSWORD
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM