如何保存上次选择的语言? [英] How to save the last selected language?

查看:128
本文介绍了如何保存上次选择的语言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am adding multiple language facility in my android app. Whenever I change the language it changes fine. But after reopening the app its again showing the choose language screen. I want to save the last selected language, so next time when user reopen the app it should not show the choose language screen it should directly go to the next page and should display the items in the language which was last selected.
What to do? Any solutions? Please check the below code. In this code where i have to store in SharedPreferences and where i have to get the sharedpreference

@Override 公共无效onItemSelected(SelectableItem selectableItem){

@Override 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);
}

推荐答案

我要保存上次选择的语言-为此,您需要使用 SharedPreferences

对于SharedPreferences参考:共享偏好

For SharedPreferences reference : shared preferences

要存储在SharedPreferences中:

To store in SharedPreferences :

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

要获取SharedPreferences:

To get the SharedPreferences :

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

在活动onCreate中检查此共享首选项是否为null.如果为null,则显示语言屏幕;如果不为null,则返回主屏幕.

Check in activity onCreate that if this shared preference is null or not. if null then language screen and if not null then home screen.

教程: SharedPreferences

有关SharedPreferences的更多信息

这篇关于如何保存上次选择的语言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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