【问题标题】:How to save the last selected language?如何保存最后选择的语言?
【发布时间】:2018-02-15 09:17:37
【问题描述】:

我正在我的 Android 应用程序中添加多种语言功能。 每当我更改语言时,它都会改变。但重新打开应用程序后,它再次显示选择语言屏幕。 我想保存最后选择的语言,所以下次当用户重新打开应用程序时,它不应该显示选择语言屏幕,它应该直接转到下一页,并且应该以最后选择的语言显示项目。
该怎么办?有什么解决办法吗? 请检查以下代码。 在这段代码中 我必须存储在 SharedPreferences 中的位置以及我必须获取 sharedpreference 的位置

@覆盖 public void onItemSelected(SelectableItem selectableItem) {

    List<Item> selectedItems = adapter.getSelectedItems();

if(selectableItem.getName().equals("English")){
    if (userSessionManager.isLoggedIn()) {
        Intent intent = new Intent(LanguageListActivity.this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        startActivity(intent);

        setLanguage("en");

    } else {
        Intent intent = new Intent(LanguageListActivity.this, LoginActivity.class);
        Log.d("Login", "firgage");
        intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        startActivity(intent);
       setLanguage("en");

    }


 }else if(selectableItem.getName().equals("Hindi(हिंदी)")){
    if (userSessionManager.isLoggedIn()) {
        Intent intent = new Intent(LanguageListActivity.this, MainHindiActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        startActivity(intent);
        setLanguage("hi");

    } else {
        Intent intent = new Intent(LanguageListActivity.this, LoginhindiActivity .class);
        Log.d("hLogin", "firhin");
        intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        startActivity(intent);
       setLanguage("hi");
        String lang = "hi";

    }

 }

}

protected void setLanguage(String language){
    mylocale=new Locale(language);
    Resources resources=getResources();
    DisplayMetrics dm=resources.getDisplayMetrics();
    Configuration conf= resources.getConfiguration();
    conf.locale=mylocale;
    resources.updateConfiguration(conf,dm);
    //Intent refreshIntent=new Intent(LanguageListActivity.this,MainActivity.class);
    finish();
    //startActivity(refreshIntent);
}

【问题讨论】:

  • 将其保存在某种配置文件中。如果您的应用程序具有登录功能,那么您可以保存为用户偏好。
  • 分享你的代码
  • 优先保存,然后在您返回应用程序后获取
  • 将所选语言保存在共享偏好中。
  • 在您添加 setLanguage("hi"); 时的代码中,通过将 hi 存储到共享首选项来添加我的 store in sharedpreference 答案

标签: android string android-studio android-activity multilingual


【解决方案1】:

我想保存最后选择的语言 - 为此您需要使用 SharedPreferences

对于 SharedPreferences 参考:shared preferences

存储在 SharedPreferences 中:

SharedPreferences sharedPref = getActivity().getPreferences(MY_PREFS_NAME,Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("lang", language);
editor.commit();

获取 SharedPreferences :

Context context = getActivity();
SharedPreferences sharedPref = context.getSharedPreferences(
       MY_PREFS_NAME, Context.MODE_PRIVATE);
String language = sharedPref.getString("lang", null); 

在活动 onCreate 中检查此共享首选项是否为空。如果为 null 则为语言屏幕,如果不为 null 则为主屏幕。

教程SharedPreferences

More about SharedPreferences

【讨论】:

    猜你喜欢
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多