我读过几次official guide,还有很多其他内容,例如本网站上的问题,但我无法让我的工作100%
截至目前,如果我在列表首选项中更改语言,则活动(SettingsActivity)不会自行更新(例如20次中的1次,某种程度上它会更新,不知道为什么).按返回按钮后,我的MainActivity也没有更新.但是我所有其他活动始终显示正确的语言,并且如果我重新启动该应用程序,则可以回到SettingsActivity,与MainActivity相同.
相关零件:
主要活动:
public class MainActivity extends Activity {public static final String KEY_PREF_LANGUAGE = "pref_language";@OverrIDeprotected voID onCreate(Bundle savedInstanceState) { ... PreferenceManager.setDefaultValues(this, R.xml.preferences, false);}@OverrIDeprotected voID onResume() { super.onResume(); SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this); String languagePref_ID = sharedPref.getString(KEY_PREF_LANGUAGE, "2"); switch (languagePref_ID) { case "1": Locale localeEN = new Locale("en_US"); setLocale(localeEN); break; case "2": Locale localeHU = new Locale("hu_HU"); setLocale(localeHU); break; }}public voID setLocale(Locale locale) { Locale.setDefault(locale); Resources res = getResources(); displayMetrics dm = res.getdisplayMetrics(); Configuration conf = res.getConfiguration(); conf.locale = locale; res.updateConfiguration(conf, dm); //recreate(); //finish(); //startActivity(getIntent()); //if these are not commented, main activity wont show at start at all}
SettingsActivity
public class SettingsActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferencechangelistener {public static final String KEY_PREF_LANGUAGE = "pref_language";@OverrIDeprotected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getFragmentManager().beginTransaction() .replace(androID.R.ID.content, new SettingsFragment()) .commit(); SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences.OnSharedPreferencechangelistener Listener = new SharedPreferences.OnSharedPreferencechangelistener() { public voID onSharedPreferenceChanged(SharedPreferences prefs, String key) { if (key.equals(KEY_PREF_LANGUAGE)) { String languagePref_ID = prefs.getString(SettingsActivity.KEY_PREF_LANGUAGE, ""); switch (languagePref_ID) { case "1": Locale localeEN = new Locale("en_US"); setLocale(localeEN); break; case "2": Locale localeHU = new Locale("hu_HU"); setLocale(localeHU); break; } } } }; sharedPref.registerOnSharedPreferencechangelistener(Listener);}public voID onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { if (key.equals(KEY_PREF_LANGUAGE)) { String languagePref_ID = sharedPreferences.getString(SettingsActivity.KEY_PREF_LANGUAGE, "2"); switch (languagePref_ID) { case "1": Locale localeEN = new Locale("en_US"); setLocale(localeEN); break; case "2": Locale localeHU = new Locale("hu_HU"); setLocale(localeHU); break; } } //this doenst even get called but i need it for the implementation}public voID setLocale(Locale locale) { Locale.setDefault(locale); Resources res = getResources(); displayMetrics dm = res.getdisplayMetrics(); Configuration conf = res.getConfiguration(); conf.locale = locale; res.updateConfiguration(conf, dm); recreate(); //trIEd like 4 ways to do this, nothing really worked //finish(); //startActivity(getIntent());}}
SettingsFragment.java
public class SettingsFragment extends PreferenceFragment {@OverrIDepublic voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Load the preferences from an XML resource addPreferencesFromresource(R.xml.preferences);}}
AndroIDManifest.xml中
androID:configChanges="locale|orIEntation" //added to main and settings activity
我为此感到疯狂,这是最佳的工作状态,但是我知道这里的活动生命周期管理远非最佳,当我尝试使用这些方法时,我通常无法正常工作.我该如何进行这项工作,使其实际上在所有活动的运行时更新?
解决方法:
更改语言后,如果您也想更改语言,则需要重新启动当前活动.例如这样:
private voID restartActivity() { Intent intent = getIntent(); finish(); startActivity(intent);}
看看这里的最佳答案How to refresh activity after changing language (Locale) inside application
总结以上是内存溢出为你收集整理的使用偏好设置在运行时更改语言[android]全部内容,希望文章能够帮你解决使用偏好设置在运行时更改语言[android]所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)