我有轻松的问题,但我找不到解决方案。让我们通过Android工作室生成设置页面。这是一个字段 - 密码。
<EditTextPreference
android:defaultValue="@string/pref_default_display_password"
android:inputType="textPassword"
android:key="password"
android:maxLines="1"
android:selectAllOnFocus="true"
android:singleLine="true"
android:password="true"
android:title="@string/pref_title_display_password" />
带星号的输入没有问题。但问题是,我在屏幕上看到保存的密码:
我该如何隐藏它?
非常感谢。
答案 0 :(得分:5)
我使用OnPreferenceChangeListener解决了这个问题,它在显示首选项之前和更改时调用。我借此机会在摘要中设置密码的修改版本,方法是将文本转换为带星号的字符串
Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();
...
if (preference instanceof EditTextPreference){
// For the pin code, set a *** value in the summary to hide it
if (preference.getContext().getString(R.string.pref_pin_code_key).equals(preference.getKey())) {
stringValue = toStars(stringValue);
}
preference.setSummary(stringValue);
}
...
return true;
}
};
String toStars(String text) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < text.length(); i++) {
sb.append('*');
}
text = sb.toString();
return text;
}
答案 1 :(得分:2)
谢谢大家,目前我使用了&#34; hotfix&#34;,它隐藏了密码值。我会在将来尝试更换它。修补程序是,我只是删除密码显示:
bindPreferenceSummaryToValue(findPreference("username"));
//bindPreferenceSummaryToValue(findPreference("password"));
bindPreferenceSummaryToValue(findPreference("ip"));
bindPreferenceSummaryToValue(findPreference("port"));
答案 2 :(得分:1)
使用此
<EditTextPreference
android:inputType="textPassword"
android:key="@string/key"
android:summary="@string/summary"
android:title="@string/title" />
你想要的android:inputType
感谢
答案 3 :(得分:0)
public class PasswordPreference扩展DialogPreference
{
private String password;
private EditText passwordEditText;
private EditText confirmEditText;
public PasswordPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context, attrs);
}
public PasswordPreference(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
setDialogLayoutResource(R.layout.confirm_password);
passwordEditText = new EditText(context, attrs);
confirmEditText = new EditText(context, attrs);
}
private void detachView(View view, View newParent) {
ViewParent oldParent = view.getParent();
if (oldParent != newParent && oldParent != null)
((ViewGroup) oldParent).removeView(view);
}
@Override
public void onBindDialogView(View view) {
detachView(passwordEditText, view);
detachView(confirmEditText, view);
LinearLayout layout = (LinearLayout) view.findViewById(R.id.linearLayout);
layout.addView(passwordEditText);
layout.addView(confirmEditText);
passwordEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
@Override
public void afterTextChanged(Editable editable) {
verifyInput();
}
});
confirmEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
@Override
public void afterTextChanged(Editable editable) {
verifyInput();
}
});
String oldPassword = getSharedPreferences().getString("password", "");
passwordEditText.setText(oldPassword);
confirmEditText.setText(oldPassword);
}
private void verifyInput() {
String newPassword = passwordEditText.getText().toString();
String confirmedPassword = confirmEditText.getText().toString();
boolean passwordOk = false;
if (newPassword.equals(confirmedPassword)) {
passwordOk = true;
password = newPassword;
}
AlertDialog dialog = (AlertDialog) getDialog();
if (dialog != null)
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(passwordOk);
}
@Override
protected void onDialogClosed(boolean positiveResult) {
super.onDialogClosed(positiveResult);
if (!positiveResult)
return;
persistString(password);
}
}
<com.abax.applocker.PasswordPreference
android:background="@color/white"
android:inputType="numberPassword"
android:key="password"
android:password="true"
android:summary="Edit the Unlock password"
android:title="Edit Password" />
使用此
答案 4 :(得分:0)
我如下解决了。
resolved code
getEditText().setInputType(
TYPE_CLASS_NUMBER|
TYPE_NUMBER_VARIATION_PASSWORD|
TYPE_NUMBER_FLAG_DECIMAL);
related code
TextView class
private static boolean isPasswordInputType(int inputType) {
int variation = inputType & 4095;
return variation == 129 || variation == 225 || variation == 18;
}
variation == 18 ->
public static final int TYPE_TEXT_VARIATION_URI = 0x00000010;
public static final int TYPE_CLASS_NUMBER = 0x00000002;