2016-12-28 60 views
1

從片段(設置)中通過按TextView調用DialogFragment,在其中更改設置(語言)應用程序,如何關閉DialogFragment,應用設置而不重新啓動整個應用程序?Android在應用程序內切換語言而不重新啓動

理想情況下,在SingleChoice選擇對話框上一次更改語言。重啓後一切正常。下面是代碼DialogFragment

public class LanguageDialogFragment extends DialogFragment { 

    final String[] itemsLang = {"English", "Russian", "Ukraine"}; 
    final String[] items = {"en", "ru", "uk"}; 
    String lang; 
    MainActivity activity = new MainActivity(); 

    @NonNull 
    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 

     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
     builder.setTitle("Выберите свой родной язык") 
       .setSingleChoiceItems(itemsLang, -1, new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int item) { 
         lang = items[item]; 

        } 
       }) 

       .setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         App.prefs.saveLanguage(lang); 
         App.changeLang(lang); 

         reload(); 

        } 
       }) 
       .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 

        } 
       }); 

     return builder.create(); 
    } 

    private void reload() { 
     Intent intent = getActivity().getIntent(); 
     getActivity().finish(); 
     Intent LaunchIntent = getActivity().getPackageManager().getLaunchIntentForPackage(App.context.getPackageName()); 
     startActivity(LaunchIntent); 
    } 
} 

下面是應用程序的例子,就像我想要實現:https://play.google.com/store/apps/details?id=com.funeasylearn.english

截圖從中:

enter image description here enter image description here

回答

0

嘗試代碼下面。您必須根據用戶選擇的語言創建新的區域設置,將其設置爲當前區域並重新啓動您的活動。請注意,如果在執行區域設置切換時它們處於活動狀態,則可能必須重新啓動其他活動。

public class LanguageDialogFragment extends DialogFragment { 

    private final String[] itemsLang = {"English", "Russian", "Ukraine"}; 
    private final String[] items = {"en", "ru", "uk"}; 
    private String lang; 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 

     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
     builder.setTitle("Выберите свой родной язык") 
      .setSingleChoiceItems(itemsLang, -1, new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int item) { 
        lang = items[item]; 

       } 
      }).setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        //When user submits, restart the activity in 
        //the new language 
        restartActivityInLanguage(lang); 

       } 
      }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        //Do nothing 
       } 
      }); 

     return builder.create(); 
    } 

    private void restartActivityInLanguage(String language) { 
     Locale locale = new Locale(language); 
     Configuration config = new Configuration(); 
     config.locale = locale; 
     Resources resources = getResources(); 
     resources.updateConfiguration(config, resources.getDisplayMetrics()); 
     getActivity().recreate(); 
    } 
} 
+0

不是一個完美的解決方案,應用類的靜態變量和JNI庫將不會重新初始化.. – blueware

+0

我不想becasue程序是做這樣的記錄屏幕一些任務重新啓動我的應用程序。所以沒有重新啓動應用程序是有任何解決方案的Android 7.0 – PriyankaChauhan

相關問題