Java 保存用户选择的语言,Android

Java 保存用户选择的语言,Android,java,android,eclipse,manifest,lang,Java,Android,Eclipse,Manifest,Lang,如何更改default string.xml?我可以将我的应用程序更改为五种不同的语言,但每当我关闭并重新打开应用程序时。它回到默认的英语 是否可以以某种方式更改默认文件夹“值”。例如,从默认值(英语)到值pt(葡萄牙语) 非常感谢您的帮助 您可以写入,然后从SharedReferences读取,以管理用户选择的语言 要保存: SharedPreferences shp = getSharedPreferences( "com.example.yourapp.PREFERENCE

如何更改default string.xml?我可以将我的应用程序更改为五种不同的语言,但每当我关闭并重新打开应用程序时。它回到默认的英语

是否可以以某种方式更改默认文件夹“值”。例如,从默认值(英语)到值pt(葡萄牙语)


非常感谢您的帮助

您可以写入,然后从
SharedReferences
读取,以管理用户选择的语言

要保存:

SharedPreferences shp = getSharedPreferences(
        "com.example.yourapp.PREFERENCES",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = shp.edit();
editor.putString("USER_LANGUAGE", language);
editor.commit();
要加载:

SharedPreferences shp = getSharedPreferences(
        "com.example.yourapp.PREFERENCES",Context.MODE_PRIVATE);
String language = shp.getString("USER_LANGUAGE","en");
加载语言时,请修改区域设置以使用它

Locale locale = new Locale(language);  
Locale.setDefault(locale); 
Configuration config = new Configuration(); 
config.locale = locale; 
getResources().updateConfiguration(config, getResources().getDisplayMetrics());
您可以使用以下命令

public void loadLocale() {
        String langPref = "Language";
        SharedPreferences prefs = getSharedPreferences("CommonPrefs",
                Activity.MODE_PRIVATE);
        String language = prefs.getString(langPref, "");
        changeLang(language);
    }

public void changeLang(String lang) {
        if (lang.equalsIgnoreCase(""))
            return;
        myLocale = new Locale(lang);
        saveLocale(lang);
        Locale.setDefault(myLocale);
        android.content.res.Configuration config = new android.content.res.Configuration();
        config.locale = myLocale;
        getBaseContext().getResources().updateConfiguration(config,getBaseContext().getResources().getDisplayMetrics());

    }

public void saveLocale(String lang) {
        String langPref = "Language";
        SharedPreferences prefs = getSharedPreferences("CommonPrefs",
                Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString(langPref, lang);
        editor.commit();
    }
在onCreate()中

您可以尝试以下方法:

要更改:

public void changeLanguage(String language) {
    locale = new Locale(language);
    saveLocale(language);
    Locale.setDefault(locale);
    conf.locale = locale;
    getBaseContext().getResources().updateConfiguration(conf, null);
    updateText();
}
要保存:

    public void saveLocale(String language) {
    SharedPreferences sharedPreferences = getSharedPreferences("com.example.myapp.PREFERENCES", Activity.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString("USER_LANGUAGE", language);
    editor.commit();
}
要更新:

    private void updateText() {
    btn_chat.setText(R.string.live_chat);
}
要加载:

public void LoadLanguage(){
    SharedPreferences shp = getSharedPreferences(
            "com.example.myapp.PREFERENCES",Context.MODE_PRIVATE);
    String language = shp.getString("USER_LANGUAGE","");
    Locale locale = new Locale(language);
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    getResources().updateConfiguration(config, getResources().getDisplayMetrics());
}
在MainActivity中,您可以检查应用程序使用的默认语言:

language = getResources().getString(R.string.lang);
然后,如果该语言是通过check with语句所需的语言,则应执行
updateText()
函数:

if (language.equals("zh")) {
        btn_lang.setText(R.string.someName);
        updateText();
    }

不要忘记在MainActivity中执行
LoadLanguage()
,以加载用户保存的语言。

以下是一些适用于我的代码:

public class  MainActivity extends AppCompatActivity {
public static String storeLang;

@Override
protected void onCreate(Bundle savedInstanceState) {

    SharedPreferences shp = PreferenceManager.getDefaultSharedPreferences(this);
    storeLang = shp.getString(getString(R.string.key_lang), "");

    // Create a new Locale object
    Locale locale = new Locale(storeLang);

    // Create a new configuration object
    Configuration config = new Configuration();
    // Set the locale of the new configuration
    config.locale = locale;
    // Update the configuration of the Accplication context
    getResources().updateConfiguration(
            config,
            getResources().getDisplayMetrics()
    );

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}


来源:

嗨,安德鲁,我想保存,就像你给我展示的使用SharedReferences一样。但保存后,它会在再次打开应用程序时自动选择新的默认语言。例如,如果我改为葡萄牙语,当我重新打开应用程序时,所有字符串都会自动从pt、string.xml值中检索出来。谢谢您的帮助。我已经用甘普塔的解决方案解决了我的问题。嗨,甘帕特,我试过你的解决方案,效果不错。主菜单和其他活动使用不同的语言,但主活动上的按钮和文本视图仍使用英语。很奇怪。这就像应用程序以默认语言英语加载主要活动(文本视图和按钮),然后将语言更改为用户选择的任何语言(葡萄牙语pt)。所以,其他的都是葡萄牙语。我想我已经修好了,但不确定这是不是最好的方法。我把“loadLocale();”放在“super.onCreate(savedInstanceState);”之前,感谢您的帮助。您好。谢谢你的回答。但是你能解释一下这个代码是如何解决这个问题的吗。谢谢。很抱歉,显然,我把问题的英文翻译错了
public class  MainActivity extends AppCompatActivity {
public static String storeLang;

@Override
protected void onCreate(Bundle savedInstanceState) {

    SharedPreferences shp = PreferenceManager.getDefaultSharedPreferences(this);
    storeLang = shp.getString(getString(R.string.key_lang), "");

    // Create a new Locale object
    Locale locale = new Locale(storeLang);

    // Create a new configuration object
    Configuration config = new Configuration();
    // Set the locale of the new configuration
    config.locale = locale;
    // Update the configuration of the Accplication context
    getResources().updateConfiguration(
            config,
            getResources().getDisplayMetrics()
    );

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);