轮换活动后重新设置默认区域设置

时间:2017-02-28 06:48:45

标签: android android-activity rotation locale imagebutton

早上好, 我已经尝试了很多解决方案,但我的应用程序中没有任何功能。 旋转之后,当我开始一个新的活动时,它设置了默认的语言环境。 请帮助我,我绝望了,我尝试了很多解决方案! :( 这是我的区域设置活动:

    ImageButton inglese = (ImageButton) findViewById (R.id.inglese);    
    ImageButton italiano = (ImageButton) findViewById (R.id.italiano);  

        inglese.setOnClickListener(onClickListener);
        italiano.setOnClickListener(onClickListener);
    }

    private OnClickListener onClickListener = new OnClickListener() {
        @Override
        public void onClick(final View v) {
            switch(v.getId()){
                case R.id.inglese:
                     //DO something
                    setLocaleEng();

                break;
                case R.id.italiano:
                     //DO something
                    setLocaleIta();
                break;

            }
        }
    };




private void setLocaleEng() {
    // TODO Auto-generated method stub
    Locale localeEng = new Locale("en");
    Locale.setDefault(localeEng);
    Configuration configEng = new Configuration();
    configEng.locale = localeEng;
    getBaseContext().getResources().updateConfiguration(configEng, getBaseContext().getResources().getDisplayMetrics());
    Intent intent = new Intent(Lingua.this, MainActivity.class);
    startActivity(intent);
    Lingua.this.finish();


}



private void setLocaleIta() {
    // TODO Auto-generated method stub
    // Locale localeIta = new Locale("it");
     Configuration newConfig = new Configuration();
     newConfig.locale = Locale.ITALIAN;
     onConfigurationChanged(newConfig);
        Intent intent = new Intent(Lingua.this, MainActivity.class);
       // Lingua.this.finish();
        startActivity(intent);
        Lingua.this.finish();
}

这是一个扩展应用程序的活动:

public class MyApplication extends Application {
@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    setLocaleEng();
    setLocaleIta();
}

private void setLocaleEng() {
    Locale locale = new Locale("en");
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    getBaseContext().getResources().updateConfiguration(config,
          getBaseContext().getResources().getDisplayMetrics());
}



private void setLocaleIta() {
    Locale localeita = new Locale("it");
    Locale.setDefault(localeita);
    Configuration config2 = new Configuration();
    config2.locale = localeita;
    getBaseContext().getResources().updateConfiguration(config2,
          getBaseContext().getResources().getDisplayMetrics());
}

}

拜托,帮助我!!

0 个答案:

没有答案