私はアンドロイドアプリを開発することにnoobです。私は尋ねたい。私のPreferenceActivityがMainActivityに戻り、PreferenceActivityに戻らずにどのように更新したいのですか?ここでは、PreferenceActivityのテーマを変更するための機能をいくつか紹介します。これは私のPreferenceActivityです:PreferenceActivity SharedPreferencesとUpdateUIの違い
パブリッククラスSettingsPreferenceが延び{
SwitchPreference themeSwitch;
String myPref = "preferences";
SharedPreferences.Editor editor;
String summary;
int theme;
@Override
public void onCreate(Bundle savedInstanceState)
{
// TODO: Implement this method
final SharedPreferences.Editor editor = getSharedPreferences(myPref, MODE_PRIVATE).edit();
final SharedPreferences prefs = getSharedPreferences(myPref, MODE_PRIVATE);
final String summary = prefs.getString("stringSummary", "Default theme");
final int theme = prefs.getInt("intTheme", (android.R.style.Theme_DeviceDefault_Light));
setTheme(theme);
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings_preference);
themeSwitch = (SwitchPreference) findPreference("switchTheme");
themeSwitch.setSummary(summary);
if (themeSwitch != null) {
themeSwitch.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference arg0, Object isOnObject) {
boolean isThemeOn = (Boolean) isOnObject;
if (isThemeOn) {
Toast.makeText(SettingsPreference.this, "Theme Dark ON", Toast.LENGTH_SHORT).show();
editor.putString("stringSummary", "Theme Dark ON");
editor.putInt("intTheme", (android.R.style.Theme_DeviceDefault));
editor.apply();
themeSwitch.setSummary(summary);
} else {
Toast.makeText(SettingsPreference.this, "Theme Dark OFF", Toast.LENGTH_SHORT).show();
editor.putString("stringSummary", "Theme Dark OFF");
editor.putInt("intTheme", (android.R.style.Theme_DeviceDefault_Light));
editor.apply();
themeSwitch.setSummary(summary);
}
return true;
}
});
}
}
}