我想添加一个选项来通过共享首选项更改应用程序语言。我在共享首选项中保存了“en”或“fa”等语言。
现在我需要将共享首选项数据设置为语言。我尝试使用 This code但我遇到了一些问题。
为了使用此代码,我应该将我的 Activity 上下文包装为:
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(MyContextWrapper.wrap(newBase,languageFromPrefs));
}
我编写了一个从共享首选项获取语言的方法,名为 getString(Context context,String key,String defaultValue)
,然后将该代码编辑为:
@Override
protected void attachBaseContext(Context newBase) {
lang = App.Prefs.getString(newBase, App.Prefs.LANGUAGE_KEY,"fa");
super.attachBaseContext(MyContextWrapper.wrap(newBase,lang));
}
看起来不错,但是当我运行我的应用程序时,我看到了一个奇怪的场景。第一次运行时,应用程序语言为 fa(波斯语),下次运行时它会自动更改为 en(英语)!
[注意:设备语言当前为英语]
这是我的主要 Activity :
(我评论了与我的问题无关的代码)
public class MainActivity extends AppCompatActivity {
EditText userText;
Button embellishBtn;
ImageView settings;
String lang;
@Override
protected void attachBaseContext(Context newBase) {
lang = App.Prefs.getString(newBase, App.Prefs.LANGUAGE_KEY,"fa");
super.attachBaseContext(App.wrap(newBase,lang));
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*
userText = (EditText) findViewById(R.id.textBox);
embellishBtn = (Button) findViewById(R.id.embellishButton);
settings = (ImageView) findViewById(R.id.settings);
App.SetFont.EditText(userText,this);
App.SetFont.Button(embellishBtn,this);
settings.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Toast.makeText(getBaseContext(),getString(R.string.settings),Toast.LENGTH_SHORT).show();
return true;
}
});
MarginActivity.isFirstRateMessage = true;
Boolean isFirstOpen = App.Prefs.getBoolean(this, App.Prefs.FIRST_OPEN_KEY,true);
if (isFirstOpen && TimeZone.getDefault().getDisplayName().toLowerCase().equals("iran standard time"))
{
App.Prefs.setString(this, App.Prefs.LANGUAGE_KEY,"fa");
App.Prefs.setBoolean(this,App.Prefs.FIRST_OPEN_KEY,false);
startActivity(new Intent(this, MainActivity.class));
finish();
}
*/
}
/*
public void onSettingsClick(View view)
{
startActivity(new Intent(this, SettingsActivity.class));
}
public void onEmbellishClick(View view)
{
if(userText.getText().toString().trim().length() > 0)
{
Intent intent = new Intent(this,EmbellishActivity.class);
intent.putExtra("user_text",userText.getText().toString() + " ");
startActivity(intent);
}
else
{
Animation shake = AnimationUtils.loadAnimation(this,R.anim.focus_zoom);
userText.startAnimation(shake);
}
userText.setText(null);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("user_text",userText.getText().toString());
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
userText.setText(savedInstanceState.getString("user_text"));
}
*/
}
和 getString 方法(App.Prefs.getString()
):
static String getString(Context context,String key,String defaultValue)
{
SharedPreferences shared = context.getSharedPreferences("Prefs", MODE_PRIVATE);
return shared.getString(key, defaultValue);
}
我的应用程序运行时没有任何错误。 我应该做什么来解决这个问题?怎么了?
最佳答案
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(MyContextWrapper.wrap
(newBase,languageFromPrefs));
}
这应该在基本 Activity 中完成,该 Activity 由项目中的每个 Activity 扩展。
关于java - 如何使用上下文包装器在运行时更改应用程序语言?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46251861/