2016-10-11 71 views
0

因此,在我的應用程序中,用戶看到的第一個活動是選擇語言。爲什麼用戶在我的應用程序中更改語言時android:label的文本不會更改?

假設用戶選擇法語,然後轉到ActivityB,然後轉到ActivityC。

現在決定改變語言。

因此返回到ActivityB,然後返回到第一個Activity並選擇西班牙語。

現在當用戶轉到ActivityB時,片段/活動中的所有其他文本都會更改爲西班牙語,但android:label仍然保留爲法語。如何解決這個問題?

這是我ActivityA外觀

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          final Bundle savedInstanceState) { 

     View rootView = inflater.inflate(R.layout.fragment_choose_language, container, false); 
     radioGroup = (RadioGroup) rootView.findViewById(R.id.lang_choice_radio); 
     radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() 
     { 
      public void onCheckedChanged(RadioGroup group, int checkedId) { 
       switch(checkedId){ 
        case R.id.english_lang: 
         // do operations specific to this selection 
         setLocale("en_US"); 
         Intent intentEng = new Intent(getActivity(), Choose_Country.class); 
         startActivity(intentEng); 
         break; 

        case R.id.indonesian_lang: 
         // do operations specific to this selection 
         setLocale("in"); 
         Intent intent = new Intent(getActivity(), Choose_Country.class); 
         startActivity(intent); 
         break; 
       } 
      } 
     }); 
     return rootView; 
    } 

    public void setLocale(String lang) { 
     myLocale = new Locale(lang); 
     Resources res = getResources(); 
     DisplayMetrics dm = res.getDisplayMetrics(); 
     Configuration conf = res.getConfiguration(); 
     conf.locale = myLocale; 
     res.updateConfiguration(conf, dm); 
     Locale.setDefault(myLocale); 
     onConfigurationChanged(conf); 
     Intent refreshIntent = new Intent(getActivity(), ChooseLanguage.class); // refresh the activity 
     refreshIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 
     refreshIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     startActivity(refreshIntent); 
     getActivity().finish(); 
    } 
+1

+0

這就是它的原理。 – user3705478

+0

您正在完成此行上的活動getActivity()。finish();那麼用戶如何導航回[從活動B]到這個活動[A]? –

回答

1

在我的情況下,應用程序將不會改變改變區域後,動作條的語言。它會改變,當我從最近的應用程序,使應用程序完全關閉的應用程序。爲了解決這個問題,我想在刷新應用程序時使用setTitle(R.id.myapplabel),或者使用創建,所以不需要重新啓動應用程序。在string.xml上翻譯你的活動標籤,它應該可以工作。

+0

現在我有另一個問題。在ActivityA中選擇法語後,我去了ActivityB,我看到了所有法語。但是如果我旋轉手機,整個片段就會變成英文(這是默認設置)。我搜索了使用android:confgChanges =「orientation | keyboard | locale」的Stackoverflow答案。但它不起作用。 – user3705478

+0

當你旋轉屏幕時,你的應用程序再次重新啓動。所以要處理這個問題,你必須把你的setLocale方法和setTitle放到oncreate中的activity標籤中 –

相關問題