共有環境設定でアプリケーション言語を変更するオプションを追加したかったのです。共有環境設定で「en」や「fa」のような言語を保存しました。コンテキストラッパーを使用して実行時にアプリケーション言語を変更するにはどうすればよいですか?
これで、言語として共有設定データを設定する必要があります。私はThis codeを使用しようとしましたが、いくつか問題がありました。このコードを使用するための
私は私の活動のコンテキストをラップする必要があります:私はgetString(Context context,String key,String defaultValue)
と呼ばれる共有環境設定から言語を取得するための方法を書いて、その後にそのコードを編集した
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(MyContextWrapper.wrap(newBase,languageFromPrefs));
}
:
@Override
protected void attachBaseContext(Context newBase) {
lang = App.Prefs.getString(newBase, App.Prefs.LANGUAGE_KEY,"fa");
super.attachBaseContext(MyContextWrapper.wrap(newBase,lang));
}
それは良いようだが、私は私のアプリを実行したときに私は奇妙なシーンを見た。最初の実行時に、アプリケーション言語はfa(ペルシャ語)で、次に自動的にen(英語)に変更されました。
[注:デバイスの言語は現在英語です]
ここでは私のMainActivityです:
(私は私の問題に関連していないコードをコメント)
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);
}
私のアプリケーションはエラーなしで実行されます。 この問題を解決するにはどうすればよいですか?どうしましたか?
下記のリンクを確認してください:https://stackoverflow.com/questions/40221711/android-context-getresources-updateconfiguration-deprecated/40704077#40704077 – nomag
@nomag [LocaleHelper.java](https://gist.github。 com/muhammad-naderi/0ff264e6cc07df904bc88a9f7efbe57d)は、[This one](https://stackoverflow.com/questions/40221711/android-context-getresources-updateconfiguration-deprecated/40704077#40704077)と実際は同じです。 –