android 语言选则,应用内切换语言不重启应用的方法。

切换方法

    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);
    }
调用

@Override
protected void onResume() {
    super.onResume();
    updataLanguage("");
}

每次在activity

onResume

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


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

  builder.setPositiveButton(R.string.确定, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                Log.e("点击","第"+clanguage+"行");
                SharedPreferences sp =getSharedPreferences("语言",MODE_PRIVATE);
                switch (clanguage){
                    case 0:
                        sp.edit().putString("name","zhCN").commit();
                        updataLanguage("zhCN");
                        break;
                    case 1:
                        sp.edit().putString("name","zhTW").commit();
                        updataLanguage("zhTW");
                        break;
                    case 2:
                        sp.edit().putString("name","enUS").commit();
                        updataLanguage("enUS");
//                        showsanc(b1);
                        break;
                    case 3:


你可能感兴趣的:(android 语言选则,应用内切换语言不重启应用的方法。)