Java 更改语言按钮
我正在尝试创建一个更改应用程序语言的按钮。 我为该语言创建了字符串文件,并尝试使用以下代码更改默认语言,但没有成功:Java 更改语言按钮,java,android,button,locale,Java,Android,Button,Locale,我正在尝试创建一个更改应用程序语言的按钮。 我为该语言创建了字符串文件,并尝试使用以下代码更改默认语言,但没有成功: public void changeLang(Context context, String language) { Locale locale = new Locale(language); Locale.setDefault(locale); Configuration config = context.getResources().getConfig
public void changeLang(Context context, String language) {
Locale locale = new Locale(language);
Locale.setDefault(locale);
Configuration config = context.getResources().getConfiguration();
config.setLocale(locale);
context.createConfigurationContext(config);
context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
restartActivity();
}
private void restartActivity() {
Intent intent = getIntent();
finish();
startActivity(intent);
}
这就是我所说的:
changeLang(getApplicationContext(),"iw");
谢谢大家! 有几个选项和库很有帮助,请阅读以下内容: 我曾经使用过这个库(Android 9),并且工作得非常完美:
因此,我发现这段代码在几秒钟内就解决了我的问题: 我创建了一个新的Java文件名“MyContextWrapper”,并在其中粘贴了以下代码:
/**
* Created by hosam azzam on 07/01/2017.
* CopyRights to Bassel Mourjan
*/
import android.annotation.TargetApi;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.res.Configuration;
import android.os.Build;
import java.util.Locale;
public class MyContextWrapper extends ContextWrapper {
public MyContextWrapper(Context base) {
super(base);
}
@SuppressWarnings("deprecation")
public static ContextWrapper wrap(Context context, String language) {
Configuration config = context.getResources().getConfiguration();
Locale sysLocale = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
sysLocale = getSystemLocale(config);
} else {
sysLocale = getSystemLocaleLegacy(config);
}
if (!language.equals("") && !sysLocale.getLanguage().equals(language)) {
Locale locale = new Locale(language);
Locale.setDefault(locale);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
setSystemLocale(config, locale);
} else {
setSystemLocaleLegacy(config, locale);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
context = context.createConfigurationContext(config);
} else {
context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
}
}
return new MyContextWrapper(context);
}
@SuppressWarnings("deprecation")
public static Locale getSystemLocaleLegacy(Configuration config){
return config.locale;
}
@TargetApi(Build.VERSION_CODES.N)
public static Locale getSystemLocale(Configuration config){
return config.getLocales().get(0);
}
@SuppressWarnings("deprecation")
public static void setSystemLocaleLegacy(Configuration config, Locale locale){
config.locale = locale;
}
@TargetApi(Build.VERSION_CODES.N)
public static void setSystemLocale(Configuration config, Locale locale){
config.setLocale(locale);
}
}
在活动中:
private String LANG_CURRENT = "en";
findViewById(R.id.change).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (LANG_CURRENT.equals("en")) {
changeLang(MainActivity.this, "iw");
} else {
changeLang(MainActivity.this, "en");
}
finish();
startActivity(new Intent(MainActivity.this, MainActivity.class));
}
});
public void changeLang(Context context, String lang) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("Language", lang);
editor.apply();
}
@Override
protected void attachBaseContext(Context newBase) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(newBase);
LANG_CURRENT = preferences.getString("Language", "en");
super.attachBaseContext(MyContextWrapper.wrap(newBase, LANG_CURRENT));
}
它可以更改语言并保存到SharePreFrence
来源-嘿,谢谢你的重播。我一直在尝试使用第一个链接,但没有成功,第二个链接,我没有设法安装它。。