0

我从上周开始尝试这一切。我试图更改微调器活动的语言。虽然我可以更改语言,但是一旦我更改语言 MainActivity 会继续刷新。我的观点是 refresh() 函数有问题。任何指针或我做错了什么。这是我的函数:-

public boolean onNavigationItemSelected(int position, long id) {
 if(position == 0){
            setLocale("fr");              
              Toast.makeText(MainActivity.this, "Locale in French !", Toast.LENGTH_LONG).show();  
          }
          if(position == 1){
              setLocale("es");
              Toast.makeText(MainActivity.this, "Locale in Spain !", Toast.LENGTH_LONG).show();  
          }
          if(position == 2){
              setLocale("en");
              Toast.makeText(MainActivity.this, "Locale in English !", Toast.LENGTH_LONG).show();  
          }
          return true;

}

public void setLocale(String localeCode){
        Locale locale = new Locale(localeCode);
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
        refresh();
        //Toast.makeText(getApplicationContext(),Locale.getDefault().getDisplayLanguage(),Toast.LENGTH_LONG).show();  

    }

  private void refresh() {
      finish();
       //setContentView(R.layout.activity_main);
      Intent myIntent = new Intent(this, MainActivity.class);

     // Intent myIntent = new Intent(this, OpertingSystemFragment.class);
      startActivity(myIntent);
  }
4

2 回答 2

1

这是上述问题的解决方案

private void setAdapterToSpinner() {
    languageList.clear();
    languageList.add("Select language");
    languageList.add("English");
    languageList.add("हिंदी");
    languageAdapter=new LanguageAdapter(context,languageList);
    spinnerLanguage.setAdapter(languageAdapter);
    spinnerLanguage.setSelection(SharedPreferenceHelper.getSharedPreferenceInt(context,"selection",0));
    spinnerLanguage.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            String currentLanguage= SharedPreferenceHelper.getSharedPreferenceString(context,"Language","en");
          if(i==2){
              if(currentLanguage!="hi") {
                  setLocaleLanguage("hi");
                  SharedPreferenceHelper.setSharedPreferenceString(context, "Language", "hi");
                  SharedPreferenceHelper.setSharedPreferenceInt(context,"selection",2);
                  FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.loginFragmentContainer, new LoginFragment(), "");
                  fragmentTransaction.commit();
              }
          }else if(i==1){
              if(currentLanguage!="en") {
                  setLocaleLanguage("en");
                  SharedPreferenceHelper.setSharedPreferenceInt(context,"selection",1);
                  SharedPreferenceHelper.setSharedPreferenceString(context, "Language", "en");
                  FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.loginFragmentContainer, new LoginFragment(), "");
                  fragmentTransaction.commit();
              }

          }
        }


        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });
}

public void setLocaleLanguage(String language) {
    Locale locale = new Locale(language);
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    getActivity().getBaseContext().getResources().updateConfiguration(config, getActivity().getBaseContext().getResources().getDisplayMetrics());
}
于 2017-02-22T05:19:30.890 回答
0

你想要实现什么,你会看到什么?从代码中,我了解到完成当前活动,然后启动 MainActivity。此代码应完成活动并再次启动。

Intent intent = getIntent();
finish();
startActivity(intent);
于 2013-09-10T00:18:32.487 回答