我有一个非常简单的问题:
我有一个EditTextPreference
对话框,我想用它来获取用户的密码并且希望隐藏显示。
我应该如何做?
我有一个非常简单的问题:
我有一个EditTextPreference
对话框,我想用它来获取用户的密码并且希望隐藏显示。
我应该如何做?
以下是一个使用xml的简短示例:
<EditTextPreference
android:key="@string/key"
android:title="@string/title"
android:summary="@string/summary"
android:inputType="textPassword" />
或者您可以使用 numberPassword
替代 textPassword
。
对于 androidx
库,您应该以编程方式执行它。例如,您可以按照以下方式执行它,根据密码长度,我选择使用星号设置摘要:
[...]
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();
}
}
<EditTextPreference
android:dialogMessage="Enter your password"
android:dialogTitle="Password"
android:key="password"
android:title="Password" />
更多信息请查看developer.android
。android:inputType="numberPassword"
对我无效。Eclipse告诉我,此属性不允许使用字符串值。因此,我使用了以下内容:
<EditTextPreference
android:key="@string/key"
android:title="@string/title"
android:summary="@string/summary"
android:inputType="number"
android:password="true" />
AndroidX Preferences
,请执行以下操作:password.setOnBindEditTextListener(editText -> {
editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
});
inputType
XML属性在androidx
中未被使用。
android:inputType="textPassword"
最新的Kotlin代码版本:
class SettingsFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(com.unified.helloworld.R.xml.my_preferences, rootKey)
val pwdPref =
findPreference<EditTextPreference>("db_pwd")
if (pwdPref != null) {
pwdPref.summaryProvider = SummaryProvider<Preference?> {
val getPassword: String =
PreferenceManager.getDefaultSharedPreferences(context)
.getString("db_pwd", "Not set")!!
// Return "Not set" else return password with asterisks
if (getPassword == "Not set") {
getPassword } else { setAsterisks(getPassword.length) }
}
// Set type as password and set summary with asterisks
pwdPref.setOnBindEditTextListener(
OnBindEditTextListener { editText ->
editText.inputType =
InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
pwdPref.summaryProvider = SummaryProvider<Preference> { setAsterisks(editText.text.toString().length) }
})
}
}
// Return the password in asterisks
private fun setAsterisks(length: Int): String {
val sb = java.lang.StringBuilder()
for (s in 0 until length) {
sb.append("*") }
return sb.toString() }
}
imeOption
即可:
android:imeOptions="flagNoExtractUi"
或者以编程方式实现:
yourEditText.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI);
Preference
类,继承DialogPreference
并带有自定义的对话框。 - inazarukandroid:key="@string/key"
将其以明文形式存储。 - Dmitry