1

私は設定アクティビティを呼び出すMainActivityを持っています:

Intent i = new Intent(this, SettingsActivity.class);
startActivityForResult(i, RESULT_SETTINGS);

「device_name」は、デフォルト値または値のない EditTextPreference です。

<EditTextPreference
    android:key="device_name"
    android:title="Device Name"
    android:selectAllOnFocus="true"
    android:inputType="textCapWords"
    android:capitalize="words"
    android:singleLine="true"
    android:maxLines="1" />

My SettingsActivityPreferenceActivityを拡張します。


onPostCreate私が呼び出すbindPreferenceSummaryToValue(findPreference("device_name"));

private static void bindPreferenceSummaryToValue(Preference preference) {
    // Set the listener to watch for value changes.
    preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);

    // Trigger the listener immediately with the preference's
    // current value.
    sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
            PreferenceManager
                    .getDefaultSharedPreferences(preference.getContext())
                    .getString(preference.getKey(), ""));
}

リスナーの主な焦点は、設定の概要を設定することです。editTextPreference の場合、値が null または空であるかどうかを確認し、それを に置き換えますBuild.MODEL

private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
    @Override
    public boolean onPreferenceChange(Preference preference, Object value) {
        String stringValue = value.toString();

        if (preference instanceof ListPreference) {
            // For list preferences, look up the correct display value in
            // the preference's 'entries' list.
            ListPreference listPreference = (ListPreference) preference;
            int index = listPreference.findIndexOfValue(stringValue);

            // Set the summary to reflect the new value.
            preference.setSummary(
                    index >= 0
                            ? listPreference.getEntries()[index]
                            : null);
        }
        else if (preference instanceof EditTextPreference)
        {
            EditTextPreference editTextPreference = (EditTextPreference) preference;

            //editTextPreference.getText();

            if (stringValue.trim().equals("") || stringValue.equals(null))
            {
                Log.i("valuesUpdate", "Yes");

                editTextPreference.setText(Build.MODEL + "_1");

                Log.i("valuesUpdate", editTextPreference.getTitle() + " = " + editTextPreference.getText());

                editTextPreference.setSummary(Build.MODEL + "_2");
            }
            else
            {
                Log.i("valuesUpdate", "No?");
                editTextPreference.setSummary(stringValue);
            }
        }
        else {
            // For all other preferences, set the summary to the value's
            // simple string representation.

            preference.setSummary(stringValue);

        }
        return true;
    }
};

MainActivityから設定を取得するときは、次のコードを使用します。

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(MainActivity.getAppContext());
String android_id = sharedPreferences.getString("device_name", "UNKNOWN_DEVICE");

  1. アプリケーションを初めて起動すると、android_idは期待どおり「UNKNOWN_DEVICE」を返します。

  2. SettingsActivityを開いたり閉じたりすると、android_idは期待どおり "XT1060_1" を返します。

  3. クリックして設定"device_name"を編集すると、テキスト フィールドにも "XT1060_1" が入力されます。


問題: 設定"device_name"を空の文字列に編集し、デバイスで "OK" をクリックすると、"Device Name" の概要に期待どおり "XT1060_2" が表示され、ログに次のように表示されます。

valuesUpdate﹕ Yes
valuesUpdate﹕ Device Name = XT1060_1

(したがって、getText()setText()の直後のSettingsActivityで問題なく動作します)

しかし、"device_name"の設定を再度開いても、テキスト フィールドは空のままです。

同様に、SettingsActivityを閉じてMainActivityから設定を取得しようとすると、android_idも空の文字列を返します。

SettingsActivityを再度開くと、機能します (上記のポイント 2 と同様)。

4

1 に答える 1