如何使PreferenceActivity启动一个Dialog来设置自定义首选项

时间:2012-04-04 23:30:31

标签: android dialog sharedpreferences android-preferences

我有一个从菜单选项启动的工作首选项设置。在首选项中,我设置了一个自定义首选项,必须启动一个包含3个TextView的对话框来设置确认和更改密码。现在我不知道如何从PreferenceActivity的onPreferenceClick启动对话框。如果我听起来像个新人 - 我很抱歉!

以下是对话框弹出窗口的xml布局:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:id="@+id/root"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:id="@+id/TextView_Pwd1"
        android:text="@string/settings_oldpassword"
        android:textStyle="bold" />

    <EditText
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:id="@+id/EditText_OldPwd" />

    <TextView
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:id="@+id/TextView_Pwd1"
        android:text="@string/settings_password"
        android:textStyle="bold" />

    <EditText
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:id="@+id/EditText_Pwd1"
        android:inputType="textPassword" />

    <TextView
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:id="@+id/TextView_Pwd2"
        android:text="@string/settings_password2"
        android:textStyle="bold" />

    <EditText
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:id="@+id/EditText_Pwd2"
        android:inputType="textPassword" />

    <TextView
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:id="@+id/TextView_PwdProblem"
        android:textStyle="bold"
        android:gravity="center" />

    <TextView
        android:id="@+id/TextView_PwdProblem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/settings_pwd_not_equal" />

    <CheckBox
        android:id="@+id/checkShowPwdText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/settings_showpwd_text" />

这是对话框弹出窗口的DialogChangePassword类:

package biz.linsys.package;

import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.DialogPreference;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class DialogChangePassword extends DialogPreference {

    private String strPass1;
    private String strPass2;

    public DialogChangePassword(Context context, AttributeSet attrs) {
        super(context, attrs);
        setDialogLayoutResource(R.layout.dialog_pwdchange);
    }

    @Override
     protected void onBindDialogView(View view) {

        Dialog pwdDialog            = getDialog();
        final EditText password1    = (EditText) pwdDialog.findViewById(R.id.EditText_Pwd1);
        final EditText password2    = (EditText) pwdDialog.findViewById(R.id.EditText_Pwd2);
        final TextView error        = (TextView) pwdDialog.findViewById(R.id.TextView_PwdProblem);      

        password2.addTextChangedListener(new TextWatcher() {

            @Override
            public void afterTextChanged(Editable s) {

                strPass1 = password1.getText().toString();
                strPass2 = password2.getText().toString();

                if (strPass1.equals(strPass2)) {

                    error.setText(R.string.settings_pwd_equal);
                } else {

                    error.setText(R.string.settings_pwd_not_equal);
                }
            }  public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
               public void onTextChanged(CharSequence s, int start, int before, int count) {}
        });

        super.onBindDialogView(view);

    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {

        if(!positiveResult) return;

        SharedPreferences.Editor editor = getEditor();

        if (strPass1.equals(strPass2)) {

            editor.putString("password", strPass1);
            editor.commit();
        }

        super.onDialogClosed(positiveResult);

    }
}

这是包含自定义首选项onPreferenceClick的PreferenceActivity类。这是我需要调用对话框来更改用户密码设置的地方。

package biz.linsys.package;

import android.content.Context;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;

public class Preferences extends PreferenceActivity {

    public static Context dialogContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);

        // Get the custom preference
        Preference customPref = (Preference) findPreference("customPref");

        customPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {

            public boolean onPreferenceClick(Preference preference) {

                // [ NEED TO CALL DIALOG FROM HERE ]

                return false;
           }
        });
    }
}

1 个答案:

答案 0 :(得分:13)

这是文档中缺少的内容,我发现了很多类似的问题,大多没有明确的答案。我今天遇到了同样的问题,不知怎的,我找到了解决方案,所以我将在这里总结一下我的任务,希望有人会觉得这很有用。顺便说一句,你的问题是最详细和准确的。

一般来说,您不需要手动创建对话框,只需1)创建一个DialogPreference的子类,它将处理复杂首选项的逻辑,并且2)创建一个适当类型的节点preferences.xml,因此对话框将自动生成。

Android SDK的问题在于您无法使用可视化XML编辑器添加适当的节点,您需要手动编辑该文件。

文档的问题在于它错过了这一点信息。

所以这是一步一步的解决方案:

1)创建一个DialogPreference的子类,它将处理您的特殊偏好。有关子类所需内容的详细信息,我建议this answer

2)在您的preferences.xml中创建一个Preference节点。

3)编辑preferences.xml并将Preference替换为DialogPreference子类的全名,包括包路径e。 G。 com.sample.MyPreferenceDialog。您还可以向节点添加一些属性以自定义对话框(标题,图标等),有关详细信息,请参阅this answer或DialogPreference文档。

这就是全部。您无需将OnPreferenceClickListener添加到首选项,对话框将自动显示。

注意:我并非100%确定这是使用内容的预期方式,但似乎有效。