解決android 語言國際化在橫豎屏切換後,設置的語言又變爲本地語言

  1. 摘要

    本文從項目中使用國際化語言到最後完善國際語言環境。android端有自帶的國際化語音包,所有做國際化就相對web端容易一些,那麼做國際化時只要去拿語言環境就可以了。 一,基本的怎麼使用。二、橫豎屏切換時的語言混亂。三,其實,算了沒什麼。

  2. 登陸後是選擇系統默認語言

**在ActionBar中設置語言設置,進入的時候應該有語言默認被選中。**

 `// actionBar上的按鈕點擊事件
@Override
public boolean onOptionsItemSelected(MenuItem item) {
        int roleId = item.getItemId();
        。。。。。if else 。。。。。。
        if (roleId == R.id.language_setting) { // 跳轉到設置界面
        // 拿到語言彈出框的佈局文件
        LayoutInflater inflater = LayoutInflater.from(activity);
        View layout = inflater.inflate(R.layout.language_setting,null);
        // 拿到當前系統的語言環境
        String currntLanguage = IndexActivity.this.getResources()
                .getConfiguration().locale.getLanguage();
        if(flag == null){
        //(如果語言標識flag不爲空,則說明經過了語言切換,應該用自己設置的語言)
        // 讓默認的語言選中效果
        if (currntLanguage.equalsIgnoreCase("zh")) {
            // 選定中文語言選項
            flag = CHINESE;
        } else if (currntLanguage.equalsIgnoreCase("en")) {
            // 選定英文語言選項
            flag = ENGLISH;
        } else {
            // 選定日文語言選項
            flag = JAPANESE;
        }

    }
      //設置根據標識設置默認語言被選中狀態
     ListView listView = (ListView) layout.findViewById(R.id.language_listitem);

        List<String> dataList = new ArrayList<String>();

        dataList.add("中文");

        dataList.add("English");

        dataList.add("日本語");
        /** 
                    *根據不同的語言標識設置適配不同的語言環境
                    *activity 當前Activity上下文,
                    *dataList 需要設置哪幾種語言
                    *flag 需要設置哪種語言
                    *this 聯繫當前Activity
                    **/
        adapter = new LanguageAdapter(activity, dataList , flag,this);
        listView.setAdapter(adapter);
                    //在選擇“語言設置”選項時,彈出多語言窗口。
        new AlertDialog.Builder(this)
        .setTitle(R.string.ab_msg_language_select).setView(layout)
        .show();
         }`

LanguangeAdapter中的核心代碼

 /**
 *  根據標識 設置選擇的語言環境
 */
private void settingLanguage(Integer flag){

    Log.i("ren", "當前選擇後的語言代號  ===="+tempNumber);
    if(tempNumber==0){
        // 設置本地語言爲中文
        locale = Locale.SIMPLIFIED_CHINESE;
    }else if(tempNumber == 1){
        // 設置本地語言爲英文
        locale = Locale.ENGLISH;
    }else{
        // 設置本地語言爲日文
        locale = Locale.JAPAN;
    }
    Locale.setDefault(locale);// 設置選定的語言
    Configuration config = new Configuration();
    // 更新設置
    config.locale = locale;
    indexActivity.getBaseContext().getResources().updateConfiguration(config,indexActivity.getBaseContext().getResources().getDisplayMetrics());
    indexActivity.finish();
    // 重啓當前界面
    indexActivity.startActivity(indexActivity.getIntent());
    // @toto 爲了確保下次進入程序記住當前選擇的語言,可以使用SharedPreferences
    // 來記住設置。values-en-rUS values-ja-rJP values-zh-rCN
    SharedPreferences sharedPreferences = indexActivity.getSharedPreferences("phantom",Context.MODE_PRIVATE);
    sharedPreferences.edit().putString("locale", locale.toString()).commit();
    // 設置成功後的提示信息
    if (locale == Locale.SIMPLIFIED_CHINESE) {
        Toast.makeText(indexActivity,
                R.string.ab_msg_chinese_setted, 1000).show();
    } else if (locale == Locale.ENGLISH) {
        Toast.makeText(indexActivity,
                R.string.ab_msg_english_setted, 1000).show();
    } else {
        Toast.makeText(indexActivity,
                R.string.ab_msg_japanese_setted, 1000).show();
    }
}

基本的國際化語言就是以上的思路。讓我們再來理一下思路: 1、剛登錄application時,application語言環境是本機中”設置“中的語言環境。 2、當你想要去選擇其它語言時,在設置語言中應該是有個默認語言被選中的效果。 3、在你選擇其它語言時,銷燬當前Activity,然後再重啓Activity。提示語言環境設置成功

橫豎屏切換時語言環境又自動變爲默認語言
首先我們先搞懂哪裏出錯了,在橫豎屏切換時,參考這篇博文:[android橫豎屏切換總結]
http://blog.csdn.net/jiangxinyu/article/details/8600407 問題處在橫豎屏切換是會重新獲取資源,就是把資源重新設置。

橫豎屏切換時使Activity不銷燬只是調用onConfigurationChanged方法:onConfigurationChanged方法的解釋(Lists configuration changes that the activity will handle itself. When a configuration change occurs at runtime, the activity is shut down and restarted by default, but declaring a configuration with this attribute will prevent the activity from being restarted. Instead, the activity remains running and its onConfigurationChanged() method is called.)

目前解決方案:在適配器中傳出一個標識,在onConfigurationChanged方法中再設置一遍語言環境。//在settingLanguage方法中保存設置的語言標識
private void settingLanguage(Integer flag){
    tempNumber = flag;
             //利用sharedPreferences保存一個數據
    SharedPreferences pre = indexActivity.getSharedPreferences("languageFlag", context.MODE_PRIVATE);
    Editor editor = pre.edit();
    editor.putInt("languageFlag", tempNumber);
    editor.commit();
            .......

在Activity 中的onConfgurationChanged方法中的代碼

`public void onConfigurationChanged(Configuration newConfig) {
    newConfig.locale = Locale.JAPAN;
    super.onConfigurationChanged(newConfig);
    // 取得MenuInflater,用於將xml資源轉換成Menu實例
    if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
        Log.d("IndexActivity", "現在是豎屏");
        //獲得當前選中的語言環境標識。


        //這個是拿到當前系統的語言環境,與自己設置的
        String currntLanguage = IndexActivity.this.getResources()
                .getConfiguration().locale.getLanguage();
                    //拿到適配器中設置的flag值
        SharedPreferences pre = this.getSharedPreferences("languageFlag", this.MODE_PRIVATE);
        this.flag = pre.getInt("languageFlag", 0);
        Log.i("ren", "currntLanguage == " + this.flag);
        Resources resourse = this.getResources();
        Configuration config = resourse.getConfiguration();
        if(this.flag == CHINESE){
            config.locale = Locale.SIMPLIFIED_CHINESE;
        }else if(this.flag == ENGLISH){
            config.locale = Locale.ENGLISH;
        }else if(this.flag == JAPANESE){
            config.locale = Locale.JAPAN;
        }
        // 更新設置
        Locale.setDefault(Locale.JAPAN);// 設置選定的語言
        this.getBaseContext().getResources().updateConfiguration(config,resourse.getDisplayMetrics());
        // 設置豎屏.......同上

最後不要忘記在AndroidMainfiest.xml中設置權限和在Activity中加上configchanges=“locale”

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章