2012-11-01 261 views
10

我们有一个Android应用程序,可用作远程PC程序的客户端。我们希望添加一项功能,以便PC可以指示Android应用程序在运行时更改其区域设置,即启动应用程序;把它与个人电脑沟通;稍后,PC会告诉应用程序切换到西班牙语或中文。在运行时更改语言环境?

我们已经为其各自的语言环境设置了所有布局和字符串资源。我们的应用程序是用户看到的唯一应用程序,因此如果设备的其余部分保持英语,则无关紧要。

还有另一个线程在Change language programmatically in Android,但它似乎没有得出结论。

我可以放。 。 。

Locale locale = new Locale(sTheNewLocale); 
Locale.setDefault(locale); 
Configuration config = new Configuration(); 
config.locale = locale; 
getBaseContext().getResources().updateConfiguration(config, 
     getBaseContext().getResources().getDisplayMetrics()); 

。 。 。在onCreate()之前的setContentView()但这并没有真正的帮助,如果我想在我的屏幕启动并运行后更改区域设置。有没有办法在Activity已经运行后重新加载内容视图?所以有没有切实可行的方法来更改区域设置或我必须告诉我的老板,除了在启动应用程序之前将整个设备设置为新的区域设置之外,无法完成此任务?

回答

15

由于API 11可以使用recreate这样可以使这个方法在你的活动:

private void restartInLocale(Locale locale) 
{ 
    Locale.setDefault(locale); 
    Configuration config = new Configuration(); 
    config.locale = locale; 
    Resources resources = getResources(); 
    resources.updateConfiguration(config, resources.getDisplayMetrics()); 
    recreate(); 
} 
+1

方法'updateConfiguration()'已弃用。这是一个非弃用的解决方案:http://stackoverflow.com/questions/40221711/android-context-getresources-updateconfiguration-deprecated – wilkas

+0

config.locale也被折旧 –

+0

@HasanAliKaraca这也包括在链接的答案。 – weston

3

您可以开始一个新的活动实例并退出旧活动。这是一个完整的(未经测试的)示例,您可以如何存储任何所需的语言并重新启动您的应用程序。您只需用您的首选语言拨打restartInLanguage即可。

public class YourMainActivity extends Activity { 
    private static final String APP_SHARED_PREFS = "com.example.test"; 
    private SharedPreferences settings; 
    private Editor editor; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     settings=getSharedPreferences(APP_SHARED_PREFS, Activity.MODE_PRIVATE); 
     editor=settings.edit(); 

     Locale locale = new Locale(settings.getString("lang", "default-lang")); 
     Locale.setDefault(locale); 
     Configuration config = new Configuration(); 
     config.locale = locale; 
     getResources().updateConfiguration(config, getResources().getDisplayMetrics()); 

     // your stuff... 
    } 

    public void restartInLanguage(String lang) { 
     editor.putString("lang", lang); 
     editor.commit(); 
     Intent intent = getIntent(); 
     finish(); 
     startActivity(intent); 
    } 

    // ... 
} 
+0

凡我会做到这一点的呢?程序从主要活动开始,所以这将如何改变主屏幕的区域设置?或者你的意思是从主要活动开始一个主要活动的新实例,然后让旧的主要活动自杀? (可以这样做吗?) – user316117

+0

是的,它是......你把上面的代码放在*语言改变的处理程序中,它重新启动你的主要活动。 – rekire

+0

我仍然无法解析你的答案。你是说我可以从我的主要活动中重新开始我的主要活动吗?我怎么做? – user316117

0

的解决方案是使用的setContentView和controlLanguage(你也可以从一个全局类调用此方法)设置区域设置后,在onResume方法中的EVERY活动中使用方法。 实施例:

@Override 
    public void onClick(View v) { 
     SharedPreferences.Editor editor; 
     editor = sharedPref.edit(); 
     Configuration config = new Configuration(getBaseContext().getResources().getConfiguration()); 
     String language = ""; 
     switch (v.getId()){ 
      case R.id.turkceButton: 
       editor.putString("language", "tr"); 
       language="tr"; 
       break; 
      case R.id.englishButton: 
       editor.putString("language", "en"); 
       language="en"; 
       break; 
     } 
     Locale locale = new Locale(language); 
     Locale.setDefault(locale); 
     config.locale = locale; 
     getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics()); 
     editor.commit(); 


@Override 
    protected void onResume() { 
     super.onResume(); 
     controlLanguage(getApplicationContext(), getResources()); 
     setContentView(R.layout.main); 
    } 

public void controlLanguage(Context context, Resources res){ 
     SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context); 
     String language = sharedPref.getString("language","en"); 
     Locale locale = new Locale(language); 
     Locale.setDefault(locale); 
     res.getConfiguration().locale = locale; 
     res.updateConfiguration(res.getConfiguration(), res.getDisplayMetrics()); 
    } 
1

我已经联合@weston和@rekire(均为1)和extendet它来处理此用例:

  • ActivityA => ActivityB => SettingsActivity-changeLocale

changeLocale之后SettingsActivity父活动ActivityAActivityB也应该重新创建以反映新的区域设置。

我的解决办法:ActivityA和ActivityB从LocalizedActivity,检查在onResume继承如果语言环境已发生变化,触发从LocalizedActivity继承recreate()如果有必要

所以每次活动会自动处理应用程序的特定区域的变化。

/** 
* An activity that can change the locale (language) of its content. 
* 
* Inspired by http://stackoverflow.com/questions/13181847/change-the-locale-at-runtime 
* 
* Created by k3b on 07.01.2016. 
*/ 
public class LocalizedActivity extends Activity { 
    /** if myLocale != Locale.Default : activity must be recreated in on resume */ 
    private Locale myLocale = null; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     fixLocale(this); 
     super.onCreate(savedInstanceState); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 

     // Locale has changed by other Activity ? 
     if ((myLocale != null) && (myLocale.getLanguage() != Locale.getDefault().getLanguage())) { 
      myLocale = null; 
      recreate(); 
     } 
    } 

    /** 
    * Set Activity-s locale to SharedPreferences-setting. 
    * Must be called before onCreate 
    * @param context 
    */ 
    public static void fixLocale(Context context) 
    { 
     final SharedPreferences prefs = PreferenceManager 
       .getDefaultSharedPreferences(context); 
     String language = prefs.getString(Global.PREF_KEY_USER_LOCALE, ""); 
     Locale locale = Global.systemLocale; // in case that setting=="use android-locale" 
     if ((language != null) && (!language.isEmpty())) { 
      locale = new Locale(language); // overwrite "use android-locale" 
     } 

     if (locale != null) { 
      Locale.setDefault(locale); 
      Configuration config = new Configuration(); 
      config.locale = locale; 
      Resources resources = context.getResources(); 
      resources.updateConfiguration(config, resources.getDisplayMetrics()); 
      // recreate(); 

      if (context instanceof LocalizedActivity) { 
       ((LocalizedActivity) context).myLocale = locale; 
      } 
     } 
    } 
} 

这里是LocalizedActivity.javaSettingsActivity.java源被用来在我A Photo Manager project