0

私がやりたいことは、ユーザーが EditTextPreference を変更しようとしているときにユーザーに尋ねて、[OK] をクリックすることです。特定の条件が true の場合は、設定を変更するかどうかを確認します。

したがって、条件が true の場合は、はいまたはいいえを尋ねるダイアログを表示し、ダイアログが非同期であることを示しているため、OnPreferenceChange コールバックで false を返し、設定の変更をキャンセルします。

ユーザーが設定を変更しないことにした場合、私はそれ以上何もしません。ただし、[はい] をクリックした場合は、エディターを使用して手動で設定を変更し、変更をコミットします。

ユーザーが [はい] を押した後 (設定を変更したい)、[設定] 画面で EditTextPreference をもう一度クリックすると、新しい値ではなく、EditText の古い値が使用されます。

プリファレンス画面を閉じて再度開いたときにのみ、新しい値が表示されます。

だから私の質問は、設定画面がSharedPreferencesからの新しい新しい値でビューを更新するようにするeditor.commit()の後に「はい」Dialog.OnClickListener()で呼び出すことができるメソッドはありますか?

または、私が望むものを達成する他の方法はありますか?

ありがとう

コード例:

EditTextPreference etp;
etp.setOnPreferenceChangeListener(new OnPreferenceChangeListener()
    {           
        @Override
        public boolean onPreferenceChange(final Preference preference, final Object newValue)
        {
            int a;
            int b;

            if(a < b) {
                AlertDialog.Builder builder = new AlertDialog.Builder(Preferences.this);
                Dialog changeMaxCountConfirmationDialog = builder
                        .setCancelable(true)
                        .setTitle("Change Setting")
                        .setMessage("Are you sure you want to change this setting")
                        .setPositiveButton("Yes", new Dialog.OnClickListener()
                        {
                            @Override
                            public void onClick(DialogInterface dialog, int which)
                            {
                                //Because I return false anyway, I change the preference manually here
                                Editor editor = preference.getEditor();
                                editor.putString("key", newValue.toString());
                                editor.commit();
                            }
                        }).setNegativeButton("No", null)
                        .create();
                changeMaxCountConfirmationDialog.show();
                return false;
            }

            return true;
        }
    });
4

2 に答える 2

0

遅い答えであることは知っていますが、他の人には役立つかもしれません。

Editor単に使用する代わりにEditTextPreference#setText().

つまり、コードを置き換えます

//Because I return false anyway, I change the preference manually here
Editor editor = preference.getEditor();
editor.putString("key", newValue.toString());
editor.commit();

このようなもので:

//Because I return false anyway, I change the preference manually here
((EditTextPreference)preference).setText(newValue.toString());
于 2012-12-25T11:30:02.943 に答える