切换方法

public void updataLanguage(String s){

SharedPreferences sp =getSharedPreferences("语言",MODE_PRIVATE);

String language;

if(s.equals("")){

language=sp.getString("name","zhCN");

}else{

language=s;

}

Resources resources =getResources();

Configuration config =resources.getConfiguration();

String localelanguage=config.locale.getLanguage();

String localeCountry=config.locale.getCountry();

Log.e("localelanguage",localelanguage);

Log.e("localeCountry",localeCountry);

if(!(localelanguage+localeCountry).equals(language)){

Intent intent = new Intent(this,LoginActivity.class);

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);

startActivity(intent);

}

Log.e("设置语言:","设置语言");

DisplayMetrics dm=resources.getDisplayMetrics();

switch (language){

case "zhCN":

config.locale= Locale.CHINA;

break;

case "zhTW":

config.locale= Locale.TAIWAN;

break;

case "enUS":

config.locale= Locale.US;

break;

case "koKR":

config.locale= Locale.KOREA;

break;

case "deDE":

config.locale= Locale.GERMANY;

break;

case "jaJP":

config.locale= Locale.JAPAN;

break;

case "ruRU":

config.locale= new Locale("ru", "RU");

break;

default:

config.locale= Locale.CHINA;

break;

}

resources.updateConfiguration(config,dm);

}

调用

@Overrideprotected voidonResume() {

super.onResume();updataLanguage("");}

每次在activity

onResume

的时候,如果语音已经切换了,就刷新

设置里面切换语言的时候:

builder.setPositiveButton(R.string.确定, newDialogInterface.OnClickListener() {

@Overridepublic voidonClick(DialogInterface dialog, intwhich) {

Log.e("点击","第"+clanguage+"行");SharedPreferences sp =getSharedPreferences("语言",MODE_PRIVATE);switch(clanguage){

case0:

sp.edit().putString("name","zhCN").commit();updataLanguage("zhCN");break;case1:

sp.edit().putString("name","zhTW").commit();updataLanguage("zhTW");break;case2:

sp.edit().putString("name","enUS").commit();updataLanguage("enUS");// showsanc(b1);break;case3:

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐