我正在为Englsih和印地语创建多语言应用
我使用的代码和它的工作完美,但有些东西已被弃用,请告诉我如何解决它,
已弃用的代码
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;
}
答案 0 :(得分:1)
好吧我用@Bassel Mourjan的this answer
解决了这个问题我有一个基本活动,剩下的是片段,下面是我的代码 (我在android 7.1.1(genymotion emu)中测试了这段代码并且它有效)
从上面的链接中查看(使用)MyContextWrapper类。
在我的MainActivity on按钮上单击我打开活动结果来自该活动我正在获得用户选择的语言,例如:english,hindi,marathi
MainActivity:
// 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
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
}
}