2014-09-10 116 views
3

我希望用戶能夠更改應用程序語言(字符串位於strings-de/strings.xml或strings-zh/strings.xml文件夾中)。它可以正常工作,當我將手機設置更改爲中文或其他,但我想要做的是以下幾點: 我有一個MainActivity與幾個片段,並在我的操作欄中有一個設置圖標,當用戶點擊它,一個settingsActivity打開,我有一個菜單項,用戶可以選擇他的首選語言。當用戶點擊它時,應用程序應該重新加載並使用我選擇的語言中的字符串。 我怎樣才能做到這一點?代碼屬於哪些活動?先謝謝你。在SettingsActivity中更改應用程序中的語言環境(語言)

這裏我settingsActivity:

public class SettingsActivity extends PreferenceActivity { 
    private static final boolean ALWAYS_SIMPLE_PREFS = false; 

    @Override 
    protected void onPostCreate(Bundle savedInstanceState) { 
     super.onPostCreate(savedInstanceState); 

     setupSimplePreferencesScreen(); 
    } 

    @SuppressWarnings("deprecation") 
    private void setupSimplePreferencesScreen() { 
     if (!isSimplePreferences(this)) { 
      return; 
     } 

     // In the simplified UI, fragments are not used at all and we instead 
     // use the older PreferenceActivity APIs. 

     // Add 'general' preferences. 
     addPreferencesFromResource(R.xml.pref_general); 

     // Add 'Cash-Game Ticker' preferences, and a corresponding header. 
     PreferenceCategory fakeHeader = new PreferenceCategory(this); 
     fakeHeader.setTitle(R.string.pref_header_cg_notifications); 
     getPreferenceScreen().addPreference(fakeHeader); 
     addPreferencesFromResource(R.xml.pref_cg_notification); 

     // Add 'Tournament Ticker' preferences, and a corresponding header. 
     PreferenceCategory fakeHeader2 = new PreferenceCategory(this); 
     fakeHeader2.setTitle(R.string.pref_header_tm_notifications); 
     getPreferenceScreen().addPreference(fakeHeader2); 
     addPreferencesFromResource(R.xml.pref_tm_notification); 

     // Bind the summaries of EditText/List/Dialog/Ringtone preferences to 
     // their values. When their values change, their summaries are updated 
     // to reflect the new value, per the Android Design guidelines. 
     bindPreferenceSummaryToValue(findPreference("location_list")); 
     bindPreferenceSummaryToValue(findPreference("language_list")); 
     bindPreferenceSummaryToValue(findPreference("notifications_new_cg_ringtone")); 
     bindPreferenceSummaryToValue(findPreference("notifications_new_tm_ringtone")); 
    } 

    /** {@inheritDoc} */ 
    @Override 
    public boolean onIsMultiPane() { 
     return isXLargeTablet(this) && !isSimplePreferences(this); 
    } 

    private static boolean isXLargeTablet(Context context) { 
     return (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_XLARGE; 
    } 

    private static boolean isSimplePreferences(Context context) { 
     return ALWAYS_SIMPLE_PREFS 
       || Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB 
       || !isXLargeTablet(context); 
    } 

    /** {@inheritDoc} */ 
    @Override 
    @TargetApi(Build.VERSION_CODES.HONEYCOMB) 
    public void onBuildHeaders(List<Header> target) { 
     if (!isSimplePreferences(this)) { 
      loadHeadersFromResource(R.xml.pref_headers, target); 
     } 
    } 

    /** 
    * A preference value change listener that updates the preference's summary 
    * to reflect its new value. 
    */ 
    private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() { 
     @Override 
     public boolean onPreferenceChange(Preference preference, Object value) { 
      String stringValue = value.toString(); 

      if (preference instanceof ListPreference) { 
       // For list preferences, look up the correct display value in 
       // the preference's 'entries' list. 
       ListPreference listPreference = (ListPreference) preference; 
       int index = listPreference.findIndexOfValue(stringValue); 

       // Set the summary to reflect the new value. 
       preference 
         .setSummary(index >= 0 ? listPreference.getEntries()[index] 
           : null); 

      } else if (preference instanceof RingtonePreference) { 
       // For ringtone preferences, look up the correct display value 
       // using RingtoneManager. 
       if (TextUtils.isEmpty(stringValue)) { 
        // Empty values correspond to 'silent' (no ringtone). 
        preference.setSummary(R.string.pref_ringtone_silent); 

       } else { 
        Ringtone ringtone = RingtoneManager.getRingtone(
          preference.getContext(), Uri.parse(stringValue)); 

        if (ringtone == null) { 
         // Clear the summary if there was a lookup error. 
         preference.setSummary(null); 
        } else { 
         // Set the summary to reflect the new ringtone display 
         // name. 
         String name = ringtone 
           .getTitle(preference.getContext()); 
         preference.setSummary(name); 
        } 
       } 

      } else { 
       // For all other preferences, set the summary to the value's 
       // simple string representation. 
       preference.setSummary(stringValue); 
      } 
      return true; 
     } 
    }; 

    private static void bindPreferenceSummaryToValue(Preference preference) { 
     // Set the listener to watch for value changes. 
     preference 
       .setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener); 

     // Trigger the listener immediately with the preference's 
     // current value. 
     sBindPreferenceSummaryToValueListener.onPreferenceChange(
       preference, 
       PreferenceManager.getDefaultSharedPreferences(
         preference.getContext()).getString(preference.getKey(), 
         "")); 
    } 

    /** 
    * This fragment shows general preferences only. It is used when the 
    * activity is showing a two-pane settings UI. 
    */ 
    @TargetApi(Build.VERSION_CODES.HONEYCOMB) 
    public static class GeneralPreferenceFragment extends PreferenceFragment { 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      addPreferencesFromResource(R.xml.pref_general); 

      // Bind the summaries of EditText/List/Dialog/Ringtone preferences 
      // to their values. When their values change, their summaries are 
      // updated to reflect the new value, per the Android Design 
      // guidelines. 
      bindPreferenceSummaryToValue(findPreference("example_text")); 
      bindPreferenceSummaryToValue(findPreference("example_list")); 
     } 
    } 

    /** 
    * This fragment shows notification preferences only. It is used when the 
    * activity is showing a two-pane settings UI. 
    */ 
    @TargetApi(Build.VERSION_CODES.HONEYCOMB) 
    public static class NotificationPreferenceFragment extends 
      PreferenceFragment { 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      addPreferencesFromResource(R.xml.pref_cg_notification); 

      // Bind the summaries of EditText/List/Dialog/Ringtone preferences 
      // to their values. When their values change, their summaries are 
      // updated to reflect the new value, per the Android Design 
      // guidelines. 
      bindPreferenceSummaryToValue(findPreference("notifications_new_message_ringtone")); 
     } 
    } 

    /** 
    * This fragment shows data and sync preferences only. It is used when the 
    * activity is showing a two-pane settings UI. 
    */ 
    @TargetApi(Build.VERSION_CODES.HONEYCOMB) 
    public static class DataSyncPreferenceFragment extends PreferenceFragment { 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      addPreferencesFromResource(R.xml.pref_tm_notification); 

      // Bind the summaries of EditText/List/Dialog/Ringtone preferences 
      // to their values. When their values change, their summaries are 
      // updated to reflect the new value, per the Android Design 
      // guidelines. 
      bindPreferenceSummaryToValue(findPreference("sync_frequency")); 
     } 
    } 
} 

這裏的XML的一部分,用戶可以選擇語言:

<ListPreference 
    android:defaultValue="en" 
    android:entries="@array/pref_language_select" 
    android:entryValues="@array/pref_language_values" 
    android:key="language_list" 
    android:negativeButtonText="@null" 
    android:positiveButtonText="@null" 
    android:title="@string/pref_title_language" /> 

這裏從語言選擇列表中的數組:

<string-array name="pref_language_select"> 
    <item>Deutsch</item> 
    <item>English</item> 
    <item>中文</item> 
    <item>Espanol</item> 
    <item>Magyar</item> 
    <item>Srpski</item> 
    <item>Nederlands</item> 
</string-array> 
<string-array name="pref_language_values"> 
    <item>de</item> 
    <item>en</item> 
    <item>zh</item> 
    <item>es</item> 
    <item>mg</item> 
    <item>sr</item> 
    <item>nl</item>   
</string-array> 

我希望有人能幫助我,先謝謝您!

+0

在我的待辦事項列表中。感謝您的詢問。 – danny117 2014-09-10 21:14:42

回答

0

有趣..try這樣的:

Configuration config = getBaseContext().getResources().getConfiguration(); 

    String lang = "fr"//get your language here as you see fit 
    if (! config.locale.getLanguage().equals(lang)) 
    { 
     locale = new Locale(lang); 
     Locale.setDefault(locale); 
     config.locale = locale; 
     getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics()); 
    } 

,如果這樣做對你的工作,然後即時通訊擔心ONCONFIG變化。處理配置更改時,您必須再次執行此確切操作。

+0

不知道該把這個放在哪裏? – 2014-09-10 21:26:41

+0

嘗試它作爲一個測試oncreate。你必須決定你想要的位置。 – j2emanue 2014-09-10 21:28:06

+0

我已經把它放在onPostCreate(),除非我沒有在我的SettingsActivity中的onCreate,導入所有的庫,但沒有任何反應,我如何使onclicklistener選擇語言?請幫忙謝謝 – 2014-09-10 21:35:29

相關問題