3

我正在尝试在 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>
4

1 回答 1

2

你需要做两件事:

  1. 在清单中添加以下内容:

    android:configChanges="locale"

  2. 在用户更改语言的设置屏幕中创建一种方法:

    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);
       }
    }
    
于 2015-07-16T02:42:43.263 回答