我有一个Spinner,当我点击,例如,德语,App被翻译成德语。当我重新启动应用程序时,我再次使用默认语言。 我现在的问题是如何保存语言,当我重新启动应用程序仍然在所选语言中更改?
我的活动代码是:
private Typeface ttf;
Spinner spinnerctrl;
Button btn;
Locale myLocale;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_einstellungen);
//Schrift & OnClickListener
ttf = Typeface.createFromAsset(getAssets(), "schrift.ttf");
((TextView)findViewById(R.id.settings_title)).setTypeface(ttf);
((TextView)findViewById(R.id.appinfo)).setTypeface(ttf);
findViewById(R.id.appinfo).setOnClickListener(this);
((TextView)findViewById(R.id.settings_back)).setTypeface(ttf);
findViewById(R.id.settings_back).setOnClickListener(this);
//Spinner Language
spinnerctrl = (Spinner) findViewById(R.id.spinner);
spinnerctrl.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if (pos == 1) {
setLocale("ar");
} else if (pos == 2) {
setLocale("en");
} else if (pos == 3) {
setLocale("de");
} else if (pos == 3) {
setLocale("nv-rUs");
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
//TODO Language translation System
//LanguageTranslation
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);
Intent refresh = new Intent(this, Settings.class);
startActivity(refresh);
finish();
}
@Override
public void onBackPressed() {
}
@Override
public void onClick(View view) {
if(view.getId()==R.id.appinfo) {
showAppinfo();
} else if(view.getId()==R.id.settings_back) {
finish();
}
}
private void showAppinfo() {
final Dialog dialog = new Dialog(this,android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
dialog.setContentView(R.layout.dialog_appinfo);
((TextView)(dialog.findViewById(R.id.appinfo_herrausgeber))).setTypeface(ttf);
((TextView)(dialog.findViewById(R.id.appinfo_name))).setTypeface(ttf);
((TextView)(dialog.findViewById(R.id.appinfo_version))).setTypeface(ttf);
((TextView)(dialog.findViewById(R.id.appinfo_developer))).setTypeface(ttf);
((TextView)(dialog.findViewById(R.id.appinfo_back))).setTypeface(ttf);
dialog.findViewById(R.id.appinfo_back).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.dismiss();
return;
}
});
dialog.show();
}
}
答案 0 :(得分:1)
好的,虽然您必须完全重启应用程序以便更改区域设置,但它仍然有效。我相信你将能够弄清楚如何让它立即做出改变; )
public class MainActivity extends AppCompatActivity
{
SharedPreferences sharedPreferences;
@Override protected void onCreate( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
sharedPreferences = PreferenceManager.getDefaultSharedPreferences( getApplicationContext( ) );
setLocale( sharedPreferences.getString( "user_locale", "en" ) );
setContentView( R.layout.activity_main );
Spinner spinner = ( Spinner ) findViewById( R.id.spinner );
spinner.setOnItemSelectedListener( new AdapterView.OnItemSelectedListener( )
{
@Override public void onItemSelected( AdapterView< ? > parent, View view, int position, long id )
{
switch( position )
{
case 1:
setLocale( "de" );
break;
case 2:
setLocale( "ru" );
break;
default:
setLocale( "en" );
break;
}
}
@Override public void onNothingSelected( AdapterView< ? > parent ) { }
} );
}
public void setLocale( String lang )
{
Locale myLocale = new Locale( lang );
if( IsLocaleValid( myLocale ) )
{
// Setting default locale to myLocale
Locale.setDefault( myLocale );
// Getting required variables
Resources res = getApplicationContext( ).getResources( );
Configuration conf = res.getConfiguration( );
// Setting required info
conf.locale = myLocale;
res.updateConfiguration( conf, null );
SharedPreferences.Editor editor = sharedPreferences.edit( );
editor.putString( "user_locale", lang ).apply( );
}
else
{
// Logging so that you know that you have error somewhere ; )
Log.w( "Locale", "Entered invalid locale: " + lang );
}
}
private boolean IsLocaleValid( Locale locale )
{
return Arrays.asList( Locale.getAvailableLocales( ) ).contains( locale );
}
}
P.S。对不起,我已经很久了,我还有其他一些非常重要的事情要做:/
P.P.S。请告诉我它是否有效:P
答案 1 :(得分:0)
SharedPreferences mylanguage;
SharedPreferences.Editor myeditor;
String lang;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_einstellungen);
mylanguage=PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
lang=mylanguage.getString("mylang","en"); //en is default when user no select
myeditor=mylanguage.edit();
if(lang.equals("ar")) {
setLocale("ar");
} else if(lang.equals("en")) {
setLocale("en");
} else if(lang.equals("de")) {
setLocale("de");
} else if(lang.equals("nv-rUs")) {
setLocale("nv-rUs");
}
}
spinnerctrl = (Spinner) findViewById(R.id.spinner);
spinnerctrl.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if (pos == 1) {
setLocale("ar");
myeditor.putString("mylang","ar");
myeditor.commit();
} else if (pos == 2) {
setLocale("en");
myeditor.putString("mylang","en");
myeditor.commit();
} else if (pos == 3) {
setLocale("de");
myeditor.putString("mylang","de");
myeditor.commit();
} else if (pos == 3) {
setLocale("nv-rUs");
myeditor.putString("mylang","nv-rUs");
myeditor.commit();
}
答案 2 :(得分:0)
答案 3 :(得分:0)
很久以前我实现了语言切换功能。它不需要重新启动应用程序来更改区域设置。要使其工作,您必须记住,在应用任何配置更新时,您必须确保所有活动都能够处理该配置更改。仅仅应用它是不够的。如您所知,Android处理配置更改的默认方式是销毁您的活动并重新创建它。您拥有的另一个选项是在清单中指明您要处理configChange
。
对于第一个选项,您只需在活动中调用recreate()
即可。那几乎就是你需要做的一切。 Android会尽可能保持父级活动的活动状态(已调用他们的onStop()
方法,但不会调用他们的onDestroy()
)。因此,这些活动也需要使用新配置重新创建。您可能已经知道如何实现它。我会留给你找到最好的方法。如果您需要提示,请查看Application.Activity LifecycleCallbacks
。
处理配置更改的默认方式不是非常用户友好。摧毁和重建活动显然是缓慢而笨重的。手动处理区域设置更改可获得最佳用户体验。但是,在您的情况下,由于您在应用区域设置更改后已经销毁了活动,因此如果您使用recreate
方法,用户体验将不会受到太大干扰。
完成所有操作后,您在应用更改后重新创建了所有活动,但仍然丢失了新配置。那就是发生在我身上的事情。如果您也是这种情况,则需要在onCreate()
中再次应用配置更新,然后再致电super.onCreate()
。