2014-09-10 2 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

답변

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

어디를 넣어야할지 모르시겠습니까?! –

+0

테스트로 생성하십시오. 당신은 당신이 그것을 원하는 곳을 결정해야 할 것입니다. – j2emanue

+0

내 SettingsActivity에서 onCreate를 사용하지 않고 모든 라이브러리를 가져 오지 않으면 않는 한 onPostCreate()에 넣었습니다.하지만 아무 것도 일어나지 않습니다. 어떻게 언어를 선택하면 onclicklistener를 만들 수 있습니까? 고맙습니다. –