如何保存用户的区域设置(应用程序)? [英] How do I save user's locale settings (in app)?

查看:196
本文介绍了如何保存用户的区域设置(应用程序)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做有两个可用的语言的应用程序。用户在语言之间改变与点击一个按钮。这里是我的onClick方法:

 公共无效setLocaleEng(视图v){
    区域设置localeEng =新的语言环境(EN);
    Locale.setDefault(localeEng);
    配置configEng =新配置();
    configEng.locale = localeEng;
    。getBaseContext()getResources()updateConfiguration(configEng,getBaseContext()getResources()getDisplayMetrics());
    意向意图=新意图(NastavitveJezika.this,MainActivity.class);
    完();
    startActivity(意向);
}公共无效setLocaleSlo(视图v){
    区域设置localeSlo =新的语言环境(SL);
    Locale.setDefault(localeSlo);
    配置configSlo =新配置();
    configSlo.locale = localeSlo;
    。getBaseContext()getResources()updateConfiguration(configSlo,getBaseContext()getResources()getDisplayMetrics());
    意向意图=新意图(NastavitveJezika.this,MainActivity.class);
    完();
    startActivity(意向);
}

这工作,因为它应该,但是当用户完全退出应用程序,并重新打开它,它会回到默认值(英文)。如何让我的应用程序记住用户选择的语言设置?如果答案是共享preferences,那么如何?我只用至今的共享preferences存储字符串和布尔值,我不知道我怎么会接近这样的事情。


解决方案

  

如果答案是共享preferences,那么如何?我只用共享
  preferences存储字符串。


是的,答案是共享preferences,你可以用它来存储字符串,就像你以前那样。只是让存储en或​​SL,然后

 字符串enOrSlReadFromShared preFS = readShared prefsJustLikeYouDidBefore();
    区域设置区域=新区域(enOrSlReadFromShared preFS);
    Locale.setDefault(区域);
    配置configSlo =新配置();
    configSlo.locale = localeSlo;
    。getBaseContext()getResources()updateConfiguration(configSlo,getBaseContext()getResources()getDisplayMetrics());
    意向意图=新意图(NastavitveJezika.this,MainActivity.class);
    完();
    startActivity(意向);

I'm making an app that has two available languages. User is changing between languages with a button click. Here are my onClick methods:

public void setLocaleEng(View v){
    Locale localeEng = new Locale("en");
    Locale.setDefault(localeEng);
    Configuration configEng = new Configuration();
    configEng.locale = localeEng;
    getBaseContext().getResources().updateConfiguration(configEng, getBaseContext().getResources().getDisplayMetrics());
    Intent intent = new Intent(NastavitveJezika.this, MainActivity.class);
    finish();
    startActivity(intent);
}

public void setLocaleSlo(View v){
    Locale localeSlo = new Locale("sl");
    Locale.setDefault(localeSlo);
    Configuration configSlo = new Configuration();
    configSlo.locale = localeSlo;
    getBaseContext().getResources().updateConfiguration(configSlo, getBaseContext().getResources().getDisplayMetrics());
    Intent intent = new Intent(NastavitveJezika.this, MainActivity.class);
    finish();
    startActivity(intent);
}

This works as it should, but when user completely exits application and re-opens it, it will be back to default (english). How do I make my app remember which language settings the user has chosen? If the answer is Shared Preferences, then how? I've only used shared preferences to store strings and booleans so far, I don't know how I would approach something like this.

解决方案

If the answer is Shared Preferences, then how? I've only used shared preferences to store strings.

Yes, answer is SharedPreferences and you can use it to store Strings, just like you did before. Just make it store "en" or "sl" and then

    String enOrSlReadFromSharedPrefs = readSharedPrefsJustLikeYouDidBefore();
    Locale locale = new Locale(enOrSlReadFromSharedPrefs);
    Locale.setDefault(locale);
    Configuration configSlo = new Configuration();
    configSlo.locale = localeSlo;
    getBaseContext().getResources().updateConfiguration(configSlo, getBaseContext().getResources().getDisplayMetrics());
    Intent intent = new Intent(NastavitveJezika.this, MainActivity.class);
    finish();
    startActivity(intent);

这篇关于如何保存用户的区域设置(应用程序)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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