0

我想阻止用户查看在edittextpreference首选项摘要中输入的密码。我已按逻辑处理它以不更新摘要,如下所示:

    if (!preference.getKey().equals(EMAIL_PWD_KEY)
                        && !preference.getKey().equals(PASSCODE_KEY))
    {
        sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,PreferenceManager.getDefaultSharedPreferences(preference.getContext())
                                                                                                  .getString(preference.getKey(),
                                                                                                             ""));
    }

但是当我点击偏好的“确定”按钮时,摘要会更新。关于防止它更新的任何想法?

4

1 回答 1

1

我找到了一种方法,方法是实现一个名为 EditTextPreferenceWithSummary 的类,它扩展了 EditTextPreference 并覆盖了一些方法......

public class EditTextPreferenceWithSummary extends EditTextPreference {

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

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

   @Override
    protected View onCreateView(ViewGroup parent) {
        updateSummary();
        return super.onCreateView(parent);
    }

    public void updateSummary()
    {
        String value = "";
        int variation = (this.getEditText().getInputType() & InputType.TYPE_MASK_VARIATION);
        Boolean isPassword = ((variation  == InputType.TYPE_NUMBER_VARIATION_PASSWORD)
                            ||(variation  == InputType.TYPE_TEXT_VARIATION_PASSWORD)
                            ||(variation  == InputType.TYPE_TEXT_VARIATION_WEB_PASSWORD));

        if (isPassword)
        {
            value = this.getText().replaceAll(".", "*");
        }
        else
        {
            value = this.getText();
        }
        this.setSummary(value);
    }

    private void init() {
        setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
             @Override
             public boolean onPreferenceChange(Preference preference, Object newValue) {
                 EditTextPreferenceWithSummary pref = (EditTextPreferenceWithSummary) preference;
                 pref.updateSummary();
                 return true;
             }
        });
    }
}

我的偏好 xml 文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

    <PreferenceCategory
            android:title="@string/pref_header_database">
            <com.designit.api.preferences.EditTextPreferenceWithSummary
                    android:title="@string/field_database_server_ip"
                    android:defaultValue=""
                    android:summary="Set the IP adress of the server"
                    android:key="server_ip_address" />
            <com.designit.api.preferences.EditTextPreferenceWithSummary
                    android:title="@string/field_database_server_port"
                    android:defaultValue=""
                    android:inputType="number"
                    android:summary="Set the port of the server"
                    android:key="server_port" />
            <com.designit.api.preferences.EditTextPreferenceWithSummary
                    android:title="@string/field_database_name"
                    android:defaultValue=""
                    android:summary="Set the database name"
                    android:key="database_name" />
            <com.designit.api.preferences.EditTextPreferenceWithSummary
                    android:title="@string/field_database_user"
                    android:defaultValue=""
                    android:summary="Set the database user"
                    android:key="database_user" />
            <com.designit.api.preferences.EditTextPreferenceWithSummary
                    android:title="@string/field_database_password"
                    android:defaultValue=""
                    android:summary="Set the database password"
                    android:key="database_passwd" 
                    android:inputType="textPassword"/>
    </PreferenceCategory>

希望这对您有所帮助。干杯。

于 2013-04-14T18:37:03.213 回答