更改语言上运行Android中 [英] Change language on runtime in Android

查看:140
本文介绍了更改语言上运行Android中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要改变语言上运行Android中(是的,我知道,这不是一个好现象,但是这是一个要求......)。

所以我有一个基本的类,从每一个活动的延伸。此类具有以下功能:

 公共静态无效changeLanguage(上下文的背景下){
    资源RES = context.getResources();
    / *
     *在应用程序更改区域设置。
     * /
    DisplayMetrics DM = res.getDisplayMetrics();

    / *
     在这preferences *存储和加载数据
     * /
    android.content.res.Configuration CONF = res.getConfiguration();
    的String [] localArray = res.getStringArray(R.array.language_short_array);
    如果(localArray!= NULL){
        共享preferences设置= context.getShared preferences(
                MyService.APP_ID,MODE_PRIVATE);
        conf.locale =新的语言环境(localArray [settings.getInt(
                preFERED_LANGUAGE_KEY,0)]);
        res.updateConfiguration(CONF,DM);
    }
}
 

我会的onCreate调用这个方法:

  @覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    changeLanguage(本);
}
 

这是超一流的。我的活动从它延伸并呼吁super.onCreate在第一。在此调用后,把他们的布局和初始化它们的设置...

我觉得我行code将使。但是,我有以下问题:有时候,性变化的语言,有时不

如果我在它和PROGRAMM暂停后设置调试断点,我preSS继续,一切工作正常。所以我认为,在某些情况下,在我的应用程序是足够慢,语言会正确地更改,而如果该应用程序是太快语言不会改变......

有我的问题的任何解决方案?我怎么能相信,我的语言将在任何时候正确地更改?

非常感谢!

编辑:这是从我的超类扩展一个类的实例

 公共类MainMenuActivity扩展BaseActivity {

@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_start);
    }
}
 

解决方案

changeLanguage(本); 只需要调用时的语言改变或当应用程序是加载... res.updateConfiguration(CONF,DM); 更新全局配置,并针对您的应用程序实例,不是你的活动实例。

当您更改了活动的区域 ,你必须重新创建活动来看看你的语言变化。这可以通过强制改变方向时再​​迫使它回到这样很容易做到:

  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
 

如果您有一个语言更改后打回你会看到古老的语言,因为的onCreate 不叫。您将在 onResume 来检测语言变化,迫使活动的重新创建

- =编辑= -

使用屏幕方向重新装入活动已被证明是在某些设备上有点马车。我现在用的这个重新加载电流活动

 公共静态无效resetScreen(活动活动){
    如果(活动!= NULL){
        如果(Build.VERSION.SDK_INT> = 11){
            activity.recreate();
        } 其他 {
            意向意图= activity.getIntent();
            activity.overridePendingTransition(0,0);
            intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
            activity.finish();

            activity.overridePendingTransition(0,0);
            activity.startActivity(意向);
        }
    }
}
 

I have to change the language on runtime in Android (yes, I know that this is not a good behaviour, but this is a requirement...).

So I have a basic class, from which every activity extends. This class has the following function:

public static void changeLanguage(Context context) {
    Resources res = context.getResources();
    /*
     * Change locale settings in the app.
     */
    DisplayMetrics dm = res.getDisplayMetrics();

    /*
     * Store and load data in this preferences
     */
    android.content.res.Configuration conf = res.getConfiguration();
    String[] localArray = res.getStringArray(R.array.language_short_array);
    if (localArray != null) {
        SharedPreferences settings = context.getSharedPreferences(
                MyService.APP_ID, MODE_PRIVATE);
        conf.locale = new Locale(localArray[settings.getInt(
                PREFERED_LANGUAGE_KEY, 0)]);
        res.updateConfiguration(conf, dm);
    }
}

I will call this method in onCreate:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    changeLanguage(this);
}

This is in the super-class. My Activities extends from it and call super.onCreate at first. After this call, they set their layout and initializes their settings...

I thought that my lines of code would make it. But I have the following problem: Sometimes, the activity changes the language and sometimes not!

If I set a debug breakpoint on it and after the programm pauses I press continue, everything works fine. So I think, in some cases, where my Application is "slow enough", the language will change correctly, whereas the language won't change if the application is too fast...

Is there any solution of my problem? How can I be sure, that my language will change correctly in any time?

Thanks a lot!

Edit: Here is an example for a class which extends from my super-class

public class MainMenuActivity extends BaseActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_start);
    }
}

解决方案

changeLanguage(this); only needs to be called when the language has changed or when the App is loaded.. res.updateConfiguration(conf, dm); updates the global config and is specific to your app instance not your activity instance.

When you change the locale in an Activity you have to recreate that Activity to see your language change. This can be easily done by forcing an orientation change then forcing it back like this:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);

If you hit back after a language change you will see the old language because onCreate is not called. You will have to detect in onResume that the language changed and force a recreate of the Activity.

-= EDIT =-

Using Screen Orientation to reload the Activity has proven to be a bit buggy on some devices. I am now using this to reload the current Activity:

public static void resetScreen(Activity activity) {
    if (activity != null) {
        if (Build.VERSION.SDK_INT >= 11) {
            activity.recreate();
        } else {
            Intent intent = activity.getIntent();
            activity.overridePendingTransition(0, 0);
            intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
            activity.finish();

            activity.overridePendingTransition(0, 0);
            activity.startActivity(intent);
        }
    }
}

这篇关于更改语言上运行Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆