How to use EditTextPreference as a masked Password

2020-05-24 19:15发布

I have a very simple question:
I have a EditTextPreference dialog which I want to use for getting the user's password and I want it to be masked.

How can I do that?

4条回答
Summer. ? 凉城
2楼-- · 2020-05-24 19:43

If you want that the password mask persist also after device rotation is suffice to add the following imeOption:

in the edit text layout android:imeOptions="flagNoExtractUi"

or programmatically yourEditText.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI);

查看更多
趁早两清
3楼-- · 2020-05-24 19:44

For androidx library you should do it programmatically for example you could do it as follows, optionally I am setting the summary with asterisks according to the length of password:

[...]
import android.os.Bundle;
import android.text.InputType;
import android.widget.EditText;

import androidx.annotation.NonNull;

import androidx.preference.EditTextPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceManager;

import static androidx.preference.EditTextPreference.*;

public class MySettingsFragment extends PreferenceFragmentCompat {
    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        setPreferencesFromResource(R.xml.preferences, rootKey);

        final EditTextPreference preference = findPreference("password");

        if (preference != null) {
            preference.setSummaryProvider(new SummaryProvider() {
                @Override
                public CharSequence provideSummary(Preference preference) {

                    String getPassword = PreferenceManager.getDefaultSharedPreferences(getContext()).getString("password", "not set");

                    //we assume getPassword is not null
                    assert getPassword != null;

                    //return "not set" else return password with asterisks
                    if (getPassword.equals("not set")) {
                        return getPassword;
                    } else {
                        return (setAsterisks(getPassword.length()));
                    }
                }
            });

            //set input type as password and set summary with asterisks the new password
            preference.setOnBindEditTextListener(
                    new OnBindEditTextListener() {
                        @Override
                        public void onBindEditText(@NonNull final EditText editText) {
                            editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                            preference.setSummaryProvider(new SummaryProvider() {
                                @Override
                                public CharSequence provideSummary(Preference preference) {
                                    return setAsterisks(editText.getText().toString().length());
                                }
                            });
                        }
                    });
        }
    }

    //return the password in asterisks
    private String setAsterisks(int length) {
        StringBuilder sb = new StringBuilder();
        for (int s = 0; s < length; s++) {
            sb.append("*");
        }
        return sb.toString();
    }
}

In xml you should have something like:

<EditTextPreference
    android:dialogMessage="Enter your password"
    android:dialogTitle="Password"
    android:key="password"
    android:title="Password" />

For more info please look at developer.android

查看更多
我想做一个坏孩纸
4楼-- · 2020-05-24 19:45

Here is a short example using xml:

<EditTextPreference
    android:key="@string/key"
    android:title="@string/title"
    android:summary="@string/summary"   
    android:inputType="textPassword" />

Or you can use numberPassword instead of textPassword.

查看更多
等我变得足够好
5楼-- · 2020-05-24 20:01

android:inputType="numberPassword" doesn't work for me. Eclipse told me, that no String values are allowed for this attribute. So i used following:

<EditTextPreference
    android:key="@string/key"
    android:title="@string/title"
    android:summary="@string/summary"   
    android:inputType="number"
    android:password="true" />

This got me a EditTextPreference with a dotted textdisplay and a number keyboard for input.

查看更多
登录 后发表回答