2015-09-25 72 views
0

我想从设置中更改应用程序的语言。如何从SettingsActivity更改应用程序的区域设置?

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); 

        ............. 

这里,设置活动里面我们怎么能访问的配置和应用程序的语言环境来改变它的语言。

仅供参考,我已将值转换为字符串。

+0

这是http://stackoverflow.com/questions/13181847/change-the-locale-at-runtime的副本? – k3b

回答

0

检查这个代码

public static final String ARABIC = "ar"; 
    public static final String ENGLISH = "en"; 
public static void changeLoc(Activity context, int flagLang) 
    { 
     Resources res = context.getResources(); 
     // Change locale settings in the app. 
     DisplayMetrics dm = res.getDisplayMetrics(); 
     android.content.res.Configuration conf = res.getConfiguration(); 

     if(flagLang == 0) 
     { 
      //english 
      conf.locale = new Locale(ENGLISH); 

     } 
     else 
     { 
      //arabic 
      conf.locale = new Locale(ARABIC); 
     } 
     res.updateConfiguration(conf, dm); 
    } 
+0

当应用程序重新启动时,此代码是否持久? – ozgur

+0

是的这段代码会迫使你的应用程序听你的设置,并忽略语言电话设置。您应该将当前的语言保存在共享首选项中,并且首先当您从共享中启动活动获取保存语言并调用此方法时。 – abozaid

相关问题