我只在我的应用程序本身进行语言设置...所以我使用Locale完成多语言功能它工作正常但现在我得到的问题就像每次我需要设置语言当应用程序重新启动所以我需要只保存一次任何人都可以就此问题向我提出建议......
// Shared Preferences for Storing Spinner Selected position
SharedPreferences pref = getSharedPreferences("Mypreference", 0);
((Spinner) this.findViewById(R.id.sp_countries)).setSelection(pref
.getInt("country", 0));
((Spinner) this.findViewById(R.id.sp_language)).setSelection(pref
.getInt("language", 0));
((Spinner) this.findViewById(R.id.sp_currency)).setSelection(pref
.getInt("currency", 0));
((Spinner) this.findViewById(R.id.sp_serviceprovider))
.setSelection(pref.getInt("serviceprovider", 0));
//在按钮上单击我正在保存共享首选项值
tv_finish.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
SharedPreferences.Editor editor_ab = AboutActivity.this
.getSharedPreferences("Mypreference", 0).edit();
editor_ab.putInt("country", ((Spinner) AboutActivity.this
.findViewById(R.id.sp_countries))
.getSelectedItemPosition());
editor_ab.putInt("language", ((Spinner) AboutActivity.this
.findViewById(R.id.sp_language))
.getSelectedItemPosition());
editor_ab.putInt("currency", ((Spinner) AboutActivity.this
.findViewById(R.id.sp_currency))
.getSelectedItemPosition());
editor_ab.putInt("serviceprovider",
((Spinner) AboutActivity.this
.findViewById(R.id.sp_serviceprovider))
.getSelectedItemPosition());
// editor_ab.commit();
editor_ab.putString("Lang", lang);
editor_ab.commit();
prefs.edit().putString("Lang", lang).commit();
// prefs.edit().putString("lang", lang).commit();
Intent i = new Intent(getApplicationContext(),
CardSale_Activity.class);
startActivity(i);
// finish();
Toast.makeText(getApplicationContext(), "Saved Successfully",
1000).show();
}
});
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
String sp1 = String.valueOf(Spinner_Country.getSelectedItem());
if (sp1.contentEquals("India")) {
List<String> listIn = new ArrayList<String>();
listIn.add("English");
ArrayAdapter<String> dataAdapterIn = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, listIn);
dataAdapterIn
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapterIn.notifyDataSetChanged();
Spinner_Language.setAdapter(dataAdapterIn);
List<String> listIn2 = new ArrayList<String>();
listIn2.add("Rupee");
ArrayAdapter<String> dataAdapterIn2 = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, listIn2);
dataAdapterIn2
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapterIn2.notifyDataSetChanged();
Spinner_Currency.setAdapter(dataAdapterIn2);
setLocale("lang");
}
if (sp1.contentEquals("France")) {
List<String> listFr = new ArrayList<String>();
listFr.add("French");
ArrayAdapter<String> dataAdapterFr = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, listFr);
dataAdapterFr
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapterFr.notifyDataSetChanged();
Spinner_Language.setAdapter(dataAdapterFr);
List<String> listFr2 = new ArrayList<String>();
listFr2.add("Euro");
ArrayAdapter<String> dataAdapterFr2 = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, listFr2);
dataAdapterFr2
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapterFr2.notifyDataSetChanged();
Spinner_Currency.setAdapter(dataAdapterFr2);
setLocale("fr");
}
if (sp1.contentEquals("Nigeria")) {
List<String> listNg = new ArrayList<String>();
listNg.add("English");
ArrayAdapter<String> dataAdapterFr = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, listNg);
dataAdapterFr
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapterFr.notifyDataSetChanged();
Spinner_Language.setAdapter(dataAdapterFr);
List<String> listNg2 = new ArrayList<String>();
listNg2.add("Naira");
ArrayAdapter<String> dataAdapterFr2 = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, listNg2);
dataAdapterFr2
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapterFr2.notifyDataSetChanged();
Spinner_Currency.setAdapter(dataAdapterFr2);
setLocale("ng");
}
if (sp1.contentEquals("Kenya")) {
List<String> listKe = new ArrayList<String>();
listKe.add("English");
ArrayAdapter<String> dataAdapterFr = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, listKe);
dataAdapterFr
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapterFr.notifyDataSetChanged();
Spinner_Language.setAdapter(dataAdapterFr);
List<String> listKe2 = new ArrayList<String>();
listKe2.add("Kenya");
ArrayAdapter<String> dataAdapterFr2 = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, listKe2);
dataAdapterFr2
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapterFr2.notifyDataSetChanged();
Spinner_Currency.setAdapter(dataAdapterFr2);
setLocale("ke");
}
}
public void setLocale(String lang) {
Locale myLocale = new Locale(lang);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration config = getBaseContext().getResources()
.getConfiguration();
Locale.setDefault(myLocale);
Configuration conf = res.getConfiguration();
config.locale = myLocale;
res.updateConfiguration(config, dm);
onConfigurationChanged(conf);
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
}
protected void onPause() {
super.onPause();
prefs = PreferenceManager.getDefaultSharedPreferences(this);
lang = prefs.getString("Lang", lang);
SharedPreferences pref = getSharedPreferences("Mypreference", 0);
SharedPreferences.Editor editor_ab = pref.edit();
editor_ab.putString("Lang", lang);
editor_ab.commit();
}
/**
* onRestart Called after your activity has been stopped, prior to it being
* started again. Always followed by onStart().
*
*/
protected void onRestart() {
super.onRestart();
SharedPreferences pref = getSharedPreferences("Mypreference", 0);
SharedPreferences.Editor editor_ab = pref.edit();
editor_ab.putString("Lang", lang);
editor_ab.commit();
prefs = PreferenceManager.getDefaultSharedPreferences(this);
lang = prefs.getString("Lang", lang);
}
@Override
public void onBackPressed() {
Intent i_settingsback = new Intent(getApplicationContext(),
HomeActivity.class);
startActivity(i_settingsback);
AboutActivity.this.finish();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:0)
public void loadLocale()
{
String langPref = "Language";
SharedPreferences prefs = getSharedPreferences("CommonPrefs", Activity.MODE_PRIVATE);
String language = prefs.getString(langPref, "");
changeLang(language);
}
public void saveLocale(String lang)
{
String langPref = "Language";
SharedPreferences prefs = getSharedPreferences("CommonPrefs", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(langPref, lang);
editor.commit();
}
public void changeLang(String lang)
{
if (lang.equalsIgnoreCase(""))
return;
myLocale = new Locale(lang);
saveLocale(lang);
Locale.setDefault(myLocale);
android.content.res.Configuration config = new android.content.res.Configuration();
config.locale = myLocale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
updateTexts();
}
在updateTexts()中,您可以将需要语言更改设置字符串的所有元素的setText()调用为“R.string.my_string”,它将具有翻译
在onResume()和onCreate()
中调用loadLocale() 当你想要改变语言时,应该调用您还可以处理配置更改
@Override
public void onConfigurationChanged(android.content.res.Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (myLocale != null){
newConfig.locale = myLocale;
Locale.setDefault(myLocale);
getBaseContext().getResources().updateConfiguration(newConfig, getBaseContext().getResources().getDisplayMetrics());
}
}