我正在尝试在Android应用中提供语言设置(英语,繁体中文和简体中文)但是当语言偏好更改时,应用没有任何更改,它仍然显示相同的语言。我在stackoverflow中提到了类似的问题,但我仍然不知道我的问题在哪里。我想寻找一些帮助找到错误。非常感谢你的帮助!
public class AppPrefActivity extends Activity {
private SharedPreferences.OnSharedPreferenceChangeListener prefListener;
SharedPreferences pref;
public static final String PREF_LANGUAGE = "language_pref";
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, new PrefsFragement()).commit();
}
public static class PrefsFragement extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener{
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preference);
}
@Override
public void onResume() {
super.onResume();
getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}
@Override
public void onPause() {
getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
super.onPause();
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if(key.equals(PREF_LANGUAGE))
{
Preference connectionPref = findPreference(key);
connectionPref.setSummary(sharedPreferences.getString(key, ""));
changeLanguagePref(sharedPreferences.getString(key, ""));
}
}
private void changeLanguagePref(Context context, String lang){
Locale locale = null;
if (lang.equals("Traditional Chinese")){
locale = new Locale("zh");//("zh_rTW");
}else if (lang.equals("Simplified Chinese")){
locale = new Locale("zh");//("zh_rCN");
}else{
locale = new Locale("en");
}
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
context.getResources().updateConfiguration(config, null);
}
}
}
的AndroidManifest.xml
<activity
android:name=".AppPrefActivity"
android:label="AppPrefActivity"
android:configChanges="locale">
<intent-filter>
<action android:name="com.proj.george.betslog.AppPrefActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
首选项数组设置
<string-array name="app_language">
<item>Traditional Chinese</item>
<item>Simplified Chinese</item>
<item>English</item>
</string-array>
答案 0 :(得分:1)
你需要做两件事:
在清单中添加以下内容:
机器人:configChanges = “区域设置”
在设置屏幕中创建一个用户更改语言的方法:
public static void updateLanguage(Context context, String selectedLanguage) {
if (!"".equals(selectedLanguage)) {
if ("English".equals(selectedLanguage)) {
selectedLanguage = "en";
} else if ("Traditional Chinese".equals(selectedLanguage)) {
selectedLanguage = "zh";
}
Locale locale = new Locale(selectedLanguage);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
context.getResources().updateConfiguration(config, null);
}
}