所有活动中的语言都没有变化

时间:2017-05-20 11:45:40

标签: java android android-preferences

我正在开发和应用多项活动。其中之一是“设置”活动,可以选择更改应用程序语言。

以下是我在“设置”活动中处理更改语言的方法:

final ListPreference language = (ListPreference) findPreference("language");
    language.setSummary(Utils.getStringPreferences(context, "not-found", "language", Utils.USER_PREFS));
    language.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            if (newValue.toString().equals("English"))
                Utils.setStringPreferences(context, "English", "language", Utils.USER_PREFS);
            else if (newValue.toString().equals("Español"))
                Utils.setStringPreferences(context, "Español", "language", Utils.USER_PREFS);
            analysis_performance.setSummary(newValue.toString());
            restartActivity();
            return false;
        }
    });

这是我用来更改语言的函数,在“setContentView(R.layout.layout);”之前的所有活动中调用。方法:

public static void updateLanguage(Context context) {
    String language = getStringPreferences(context, "not-found", "language", USER_PREFS);
    String country = null;
    if (!"not-found".equals(language)) {
        if ("English".equals(language)) {
            language = "en";
            country = "US";
        } else if ("Español".equals(language)) {
            language = "es";
            country = "ES";
        }
        Locale locale = new Locale(language, country);
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        context.getResources().updateConfiguration(config, null);
    }
}

显然它有效,但仅适用于某些活动。如果我想让所有活动都使用相同的语言,我必须在使用上述功能后重新启动应用程序。

这是活动之间的导航:

- MainActivity
    - Settings
        - Activity_1
    - Activity_2

活动正确更改了语言:Activity_1和Activity_2

活动未更改语言:MainActivity和Settings

SettingsActivity:

Context context;

private AppCompatDelegate mDelegate;

List<String> allLaunchers;
List<String> allLaunchersPackages;
BrowserSpinnerAdapter adapter;

private final String TAG = Settings.class.getSimpleName();

Resources res;

@Override
protected void onCreate(Bundle savedInstanceState) {
    getDelegate().installViewFactory();
    getDelegate().onCreate(savedInstanceState);
    super.onCreate(savedInstanceState);
    Utils.updateLanguage(this);
    setContentView(R.layout.activity_settings);
    setSupportActionBar((Toolbar) findViewById(R.id.toolbar));

    addPreferencesFromResource(R.xml.settings);

    context = this;
    res = getResources();

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    // Display navigation button
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    final ListPreference browser_selection = (ListPreference) findPreference("browser_analysis");
    fillBrowserSelection();
    browser_selection.setSummary(allLaunchers.get(allLaunchersPackages.indexOf(Utils.getStringPreferences(context, "not-found", "browser", Utils.USER_PREFS))));
    browser_selection.setEntries(allLaunchers.toArray(new CharSequence[allLaunchers.size()]));
    browser_selection.setEntryValues(allLaunchersPackages.toArray(new CharSequence[allLaunchersPackages.size()]));
    browser_selection.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            Utils.setStringPreferences(context, newValue.toString(), "browser", Utils.USER_PREFS);
            browser_selection.setSummary(allLaunchers.get(allLaunchersPackages.indexOf(newValue.toString())));
            return false;
        }
    });

    final ListPreference security_level = (ListPreference) findPreference("security_level");
    security_level.setSummary(Utils.getIntPreferences(context, -1, "security_level", Utils.USER_PREFS)+"");
    security_level.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            Utils.setIntPreferences(context, Integer.parseInt(newValue.toString()), "security_level", Utils.USER_PREFS);
            security_level.setSummary(newValue.toString());
            return false;
        }
    });

    final ListPreference analysis_performance = (ListPreference) findPreference("performance");
    analysis_performance.setSummary(Utils.getStringPreferences(context, "not-found", "performance", Utils.USER_PREFS));
    analysis_performance.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            Utils.setStringPreferences(context, newValue.toString(), "performance", Utils.USER_PREFS);
            analysis_performance.setSummary(newValue.toString());
            return false;
        }
    });

    final ListPreference language = (ListPreference) findPreference("language");
    language.setSummary(Utils.getStringPreferences(context, "not-found", "language", Utils.USER_PREFS));
    language.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            if (newValue.toString().equals("English"))
                Utils.setStringPreferences(context, "English", "language", Utils.USER_PREFS);
            else if (newValue.toString().equals("Español"))
                Utils.setStringPreferences(context, "Español", "language", Utils.USER_PREFS);
            language.setSummary(newValue.toString());
            Utils.updateLanguage(Settings.this);

            recreate();
            return false;
        }
    });
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    Log.i(TAG, "onConfigurationChanged");

    getBaseContext().getResources().updateConfiguration(newConfig, getBaseContext().getResources().getDisplayMetrics());
    setContentView(R.layout.activity_settings);
    setTitle(R.string.title_activity_settings);

    // Checks the active language
    if (newConfig.locale == Locale.ENGLISH) {
        Toast.makeText(this, "English", Toast.LENGTH_SHORT).show();
    }
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            onBackPressed();
            return true;
    }
    return super.onOptionsItemSelected(item);
}

public ActionBar getSupportActionBar() {
    return getDelegate().getSupportActionBar();
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    getDelegate().onPostCreate(savedInstanceState);
}

@Override
public void setContentView(@LayoutRes int layoutResID) {
    getDelegate().setContentView(layoutResID);
}

@Override
protected void onPostResume() {
    super.onPostResume();
    getDelegate().onPostResume();
}

@Override
protected void onStop() {
    super.onStop();
    getDelegate().onStop();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    getDelegate().onDestroy();
}

private void setSupportActionBar(@Nullable Toolbar toolbar) {
    getDelegate().setSupportActionBar(toolbar);
}

private AppCompatDelegate getDelegate() {
    if (mDelegate == null) {
        mDelegate = AppCompatDelegate.create(this, null);
    }
    return mDelegate;
}

解决方案!!!

在与Rasi聊天后,我们最终通过创建此方法来填充布局组件来解决这个问题,正如他在选项1中所推荐的那样(选项2也是解决此问题的一种方法,但我更喜欢这种方式,因此用户不喜欢我必须在应用程序中导航):

private void fillSettings() {

    setContentView(R.layout.activity_settings);
    setTitle(R.string.title_activity_settings);

    setPreferenceScreen(null);
    addPreferencesFromResource(R.xml.settings);

    context = this;
    res = getResources();

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    // Display navigation button
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    final ListPreference browser_selection = (ListPreference) findPreference("browser_analysis");
    fillBrowserSelection();
    browser_selection.setSummary(allLaunchers.get(allLaunchersPackages.indexOf(Utils.getStringPreferences(context, "not-found", "browser", Utils.USER_PREFS))));
    browser_selection.setEntries(allLaunchers.toArray(new CharSequence[allLaunchers.size()]));
    browser_selection.setEntryValues(allLaunchersPackages.toArray(new CharSequence[allLaunchersPackages.size()]));
    browser_selection.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            Utils.setStringPreferences(context, newValue.toString(), "browser", Utils.USER_PREFS);
            browser_selection.setSummary(allLaunchers.get(allLaunchersPackages.indexOf(newValue.toString())));
            return false;
        }
    });

    final ListPreference security_level = (ListPreference) findPreference("security_level");
    security_level.setSummary(Utils.getIntPreferences(context, -1, "security_level", Utils.USER_PREFS)+"");
    security_level.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            Utils.setIntPreferences(context, Integer.parseInt(newValue.toString()), "security_level", Utils.USER_PREFS);
            security_level.setSummary(newValue.toString());
            return false;
        }
    });

    final ListPreference analysis_performance = (ListPreference) findPreference("performance");
    performance = Utils.getStringPreferences(context, "not-found", "performance", Utils.USER_PREFS);
    if (performance.equals(Utils.FOREGROUND)) analysis_performance.setSummary(res.getString(R.string.text_foreground));
    else if (performance.equals(Utils.BACKGROUND)) analysis_performance.setSummary(res.getString(R.string.text_background));
    analysis_performance.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            if (newValue.equals(res.getString(R.string.text_foreground))) performance = Utils.FOREGROUND;
            else if (newValue.equals(res.getString(R.string.text_background))) performance = Utils.BACKGROUND;
            Utils.setStringPreferences(context, performance, "performance", Utils.USER_PREFS);
            Log.i(TAG, performance);
            analysis_performance.setSummary(newValue.toString());
            return false;
        }
    });

    final ListPreference language = (ListPreference) findPreference("language");
    language.setSummary(Utils.getStringPreferences(context, "not-found", "language", Utils.USER_PREFS));
    language.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            if (newValue.toString().equals("English"))
                Utils.setStringPreferences(context, "English", "language", Utils.USER_PREFS);
            else if (newValue.toString().equals("Español"))
                Utils.setStringPreferences(context, "Español", "language", Utils.USER_PREFS);
            language.setSummary(newValue.toString());
            updateLanguage(Settings.this);
            fillSettings();
            return false;
        }
    });


}

并在“onCreate”中使用它,如下所示:

@Override
protected void onCreate(Bundle savedInstanceState) {
    getDelegate().installViewFactory();
    getDelegate().onCreate(savedInstanceState);
    super.onCreate(savedInstanceState);
    fillSettings();
}

1 个答案:

答案 0 :(得分:1)

您已编写代码以onCreate()方法更改语言。因此,如果您在更改语言后返回MainActivitySettingsActivity,则无法反映,因为onCreate()永远不会调用来自堆栈的活动。要么您必须利用onResume()的{​​{1}}方法。

但是您仍然无法在UI上看到更改的值,因为View已经在屏幕上绘制。现在你有两个选择 -

  1. 检查Activity中更改的区域设置并重新绘制视图或再次初始化字符串资源。请务必直接从正在更改区域设置的活动中调用方法,因为onResume()不会调用该onResume()
  2. 将用户移至初始屏幕,然后使用新的区域设置启动应用。在这种情况下,Activity将起作用。
  3. 您可以覆盖onCreate()并检查区域设置是否已更改。如果locale已更改,则重新加载新字符串。