在Spinner上改变语言

时间:2013-09-09 22:04:03

标签: android locale android-spinner

我从上周开始尝试这一切。我试图改变旋转器活动的语言。 虽然我能够改变语言但是一旦我改变语言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);
  }

2 个答案:

答案 0 :(得分: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());
}

答案 1 :(得分:0)

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

Intent intent = getIntent();
finish();
startActivity(intent);