gpt4 book ai didi

android 语言环境和 updateConfiguration 已弃用

转载 作者:太空狗 更新时间:2023-10-29 13:53:32 24 4
gpt4 key购买 nike

我正在为英语和印地语创建多语言应用

下面是我使用的代码,它运行良好,但有些东西已被弃用,请告诉我如何解决它,

弃用的代码是

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;
}

我点击了这些链接,但我不明白... link1 , link2

最佳答案

好的,我用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com