- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在为英语和印地语创建多语言应用
下面是我使用的代码,它运行良好,但有些东西已被弃用,请告诉我如何解决它,
弃用的代码是
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
我的全部代码:(这不是完整的代码,我只是向您展示主要部分)
private Intent intent;
private Locale myLocale;
private Resources res;
private DisplayMetrics dm;
private Configuration conf;
switch(lang)
{
case "no_lang":
editor = preferences.edit();
editor.putString("set_language","en");
editor.commit();
myLocale = new Locale("en");
res = getResources();
dm = res.getDisplayMetrics();
conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
finish();
break;
case "en":
editor = preferences.edit();
editor.putString("set_language","en");
editor.commit();
myLocale = new Locale("en");
res = getResources();
dm = res.getDisplayMetrics();
conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
finish();
break;
case "hi":
editor = preferences.edit();
editor.putString("set_language","hi");
editor.commit();
myLocale = new Locale("hi");
res = getResources();
dm = res.getDisplayMetrics();
conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
finish();
break;
}
最佳答案
好的,我用 this answer 解决了它@Bassel Mourjan 的
我有一个基本 Activity ,剩下的是 fragment ,下面是我的代码(我在 android 7.1.1 (genymotion emu) 中测试了这段代码并且它有效)
从上面的链接中查看(使用)MyContextWrapper 类。
在我的 MainActivity 中单击按钮我正在为该 Activity 的结果打开 Activity 我正在获取用户选择的语言,例如:英语,印地语,马拉地语
主要 Activity :
// i use shared preference to save my language so that when user close
//app and open it, then his previous language will be set
@Override
public void attachBaseContext(Context newBase)
{
SharedPreferences preferences1 = newBase.getSharedPreferences("global",newBase.MODE_PRIVATE);
String lang=preferences1.getString("set_language","en"); // at start no language selected so default is english
super.attachBaseContext(MyContextWrapper.wrap(newBase,lang));
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
navigation_header_language.setOnClickListener(this);//Language button
}
@Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.navigation_header_language:
Intent i = new Intent(this, LanguageActivity.class);
startActivityForResult(i, 101);
break;
}
}
// when user selects language i store it in shared preference and set result
// as RESULT_OK and here in onActivityResult's result_ok i simply recreate activity
//so that attachBaseContext called again and language set as we want
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 101) {
if(resultCode == Activity.RESULT_OK)
{
this.recreate();
}
if (resultCode == Activity.RESULT_CANCELED) {
//Write your code if there's no result
}
}
}
这里是 LanguageActivity,这是我从主要 Activity 调用的结果
public class LanguageActivity extends AppCompatActivity
{
private CardView english_language,hindi_language,marathi_language;
private SharedPreferences preferences;
private SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_language);
english_language=(CardView)findViewById(R.id.english_language);
hindi_language=(CardView)findViewById(R.id.hindi_language);
marathi_language=(CardView)findViewById(R.id.marathi_language);
preferences =getSharedPreferences("global",MODE_PRIVATE);
english_language.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
editor = preferences.edit();
editor.putString("set_language","en");
editor.commit();
setResultCode();
}
});
hindi_language.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
editor = preferences.edit();
editor.putString("set_language","hi");
editor.commit();
setResultCode();
}
});
marathi_language.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
editor = preferences.edit();
editor.putString("set_language","mr");
editor.commit();
setResultCode();
}
});
}
private void setResultCode()
{
Intent returnIntent = new Intent();
setResult(Activity.RESULT_OK,returnIntent);
finish();
// when this activity close, onActivityResult method call from Mainactivity
//and there i used this.recreate() which will recreate mMainactivity an
//attachBaseContext will be called once again
}
}
关于android 语言环境和 updateConfiguration 已弃用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42535958/
我正在为英语和印地语创建多语言应用 下面是我使用的代码,它运行良好,但有些东西已被弃用,请告诉我如何解决它, 弃用的代码是 conf.locale = myLocale; res.updateConf
就在最近 context.getResources()。 updateConfiguration()已在 Android API 25 中弃用,建议使用上下文。 createConfiguration
就在最近 context.getResources()。 updateConfiguration()已在 Android API 25 中弃用,建议使用上下文。 createConfiguration
编辑 2 编辑 覆盖 attachBaseContext 方法以更新 Activity 中的上下文 protected override void AttachBaseContext(Context
就在最近,context.getResources().updateConfiguration() 已在 Android API 25 中弃用,我需要更改用户选择的应用语言。我正在使用这种方法来更改语
我正在使用这个 Android API Context.Resources.UpdateConfiguration(Configuration, DisplayMetrics),但是它已经被弃用了,所
我是一名优秀的程序员,十分优秀!