12

メニューオプションから起動した作業環境設定があります。設定で、確認を設定してパスワードを変更するために、3 つの TextViews を含むダイアログを起動する必要があるカスタム設定をセットアップしました。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;
           }
        });
    }
}
4

1 に答える 1

13

これはドキュメントに記載されていないものであり、これに関する同様の質問が多数見つかりましたが、ほとんどが明確な回答がありません。私は今日同じ問題に直面しましたが、どうにかして解決策を見つけたので、誰かがこれが役立つことを願って、ここで私の探求を要約します. ところで、あなたの質問は最も詳細で正確です。

一般的なポイントは、ダイアログを手動で作成する必要がないということです。1) 複雑な設定のロジックを処理する DialogPreference のサブクラスを作成し、2) Preferences.xml に適切なタイプのノードを作成するだけです。ダイアログが自動的に生成されます。

Android SDK の問題は、ビジュアル XML エディターを使用して適切なノードを追加できないことです。ファイルを手動で編集する必要があります。

ドキュメンテーションの問題は、このわずかな情報が欠落していることです。

したがって、ここに段階的な解決策があります:

1) 特別な設定を処理する DialogPreference のサブクラスを作成します。サブクラスで必要なものの詳細については、この回答をお勧めします。

2) Preferences.xml に Preference ノードを作成します。

3) Preferences.xml を編集し、Preference をパッケージ パスを含む DialogPreference サブクラスの完全な名前 (com.sample.MyPreferenceDialog など) に置き換えます。ノードにいくつかの属性を追加して、ダイアログ (タイトル、アイコンなど) をカスタマイズすることもできます。詳細については、この回答または DialogPreference のドキュメントを参照してください。

それで全部です。環境設定に OnPreferenceClickListener を追加する必要はありません。ダイアログは自動的に表示されます。

注: これが意図した使用方法であると 100% 確信しているわけではありませんが、機能しているようです。

于 2012-09-21T16:21:34.200 に答える