2017-08-16 36 views
0

我已經在我的android應用程序中實現了設置屏幕,有一個ListPreference允許用戶選擇語言。選擇列表中的項目時,它可以正常工作,重新創建活動並自動翻譯文字,但當按下取消按鈕時,如果用戶試圖再次更改語言,效果將不可見,但當設置屏幕已關閉,文字已完美翻譯。ListPreference取消按鈕怪異behaviour

這是我SettingsActivity與我使用的內部類建模塔片段:

public class SettingsActivity extends PreferenceActivity { 

    /** Preferences fields */ 
    private static final String PREF_LANGUAGE = "pref_language"; 

    /** 
    * Method called when the application is been started 
    * 
    * @param savedInstanceState the saved instance state 
    */ 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     final String PREF_LANGUAGE = "pref_language"; 

     super.onCreate(savedInstanceState); 

     SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this); 

     String language = "en"; 
     if (Locale.getDefault().getLanguage().equals("es")) { 
      language = "es"; 
     } 
     String languagePref = settings.getString(PREF_LANGUAGE, language); 
     Locale locale = new Locale(languagePref); 
     Locale.setDefault(locale); 
     Configuration config = new Configuration(); 
     config.locale = locale; 
     getResources().updateConfiguration(config, getResources().getDisplayMetrics()); 
     setContentView(R.layout.settings_activity); 

     SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); 
     SharedPreferences.OnSharedPreferenceChangeListener prefListener = new SharedPreferences.OnSharedPreferenceChangeListener() { 

      /** 
      * Method triggered when a shared preference changes 
      * 
      * @param sharedPreferences is the object which represents the shared preferences 
      * @param key is the key of the preference changed 
      */ 
      @Override 
      public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { 
       switch (key) { 
        case PREF_LANGUAGE: 
         Locale locale = new Locale(sharedPreferences.getString(key, "")); 
         Locale.setDefault(locale); 
         Configuration config = new Configuration(); 
         config.locale = locale; 
         getResources().updateConfiguration(config, getResources().getDisplayMetrics()); 
         recreate(); 
         break; 
       } 
      } 
     }; 

     prefs.registerOnSharedPreferenceChangeListener(prefListener); 
    } 

    /** 
    * Class which represents the fragment for the settings screen 
    */ 
    public static class GeograppPreferenceFragment extends PreferenceFragment { 

     /** 
     * Method triggered when the fragment is created 
     * 
     * @param savedInstanceState is the state of the fragment 
     */ 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      addPreferencesFromResource(settings); 
      SharedPreferences settings = getPreferenceManager().getSharedPreferences(); 

      String language = "en"; 
      if (Locale.getDefault().getLanguage().equals("es")) { 
       language = "es"; 
      } 
      String languagePref = settings.getString(PREF_LANGUAGE, language); 
      getPreferenceManager().findPreference(PREF_LANGUAGE).setDefaultValue(languagePref); 
     } 
    } 
} 

偏好XML是:

<?xml version="1.0" encoding="utf-8"?> 
<PreferenceScreen 
    xmlns:android="http://schemas.android.com/apk/res/android"> 

    <PreferenceCategory 
     android:title="@string/settings_display" /> 

     <ListPreference 
      android:key="pref_language" 
      android:title="@string/settings_language_title" 
      android:summary="@string/settings_language_summary" 
      android:entries="@array/language_options" 
      android:entryValues="@array/language_values" /> 

</PreferenceScreen> 

回答

0

最後我可以定義個人監聽到的偏好與指令setOnPreferenceChangeListener並在OnCreate方法

0

很難理解你的問題,但我想使用的onResume( )函數可以幫助你在某個地方。當你離開你的設置屏幕時,onResume()函數將被調用,這就是你可以立即更新語言的地方。

+0

刪除OnSharedPreferenceChangeListener解決這個問題,但我希望看到變化的影響,而無需離開設定畫面。當我不按下ListPreference對話框的取消按鈕時它工作正常,但是當我按下一次時,它不再起作用,直到我離開設置屏幕並再次打開它 –