我想根据下拉菜单列表更改语言。但是此代码无法正常工作。它显示了这个问题Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@41141af0.
问题出在哪里,我想不通。请任何人帮助我。
public class LanguageChangedActivity extends Activity {
/** Called when the activity is first created. */
private static String[] rangeArray = new String[] { "English", "Franch",
"Japanies" };
private Spinner rangeSpinner;
String tempRange = "";
String languageToLoad = "";
boolean isFirstTime = true;
int posit=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// rangeArray = getResources().getStringArray(R.array.rangeArray);
languageToLoad = "en"; // your language
// Locale locale = new Locale(languageToLoad);
// Locale.setDefault(locale);
// Configuration config = new Configuration();
// config.locale = locale;
// getBaseContext().getResources().updateConfiguration(config,
// getBaseContext().getResources().getDisplayMetrics());
this.setContentView(R.layout.main);
Log.w("isFirstTime onCreate","????");
// Resources res = getBaseContext().getResources();
// // Change locale settings in the app.
// DisplayMetrics dm = res.getDisplayMetrics();
// android.content.res.Configuration conf = res.getConfiguration();
// conf.locale = new Locale(languageToLoad.toLowerCase());
// res.updateConfiguration(conf, dm);
// range spinner
rangeSpinner = (Spinner) findViewById(R.id.spinnerLanguage);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, getResources()
.getStringArray(R.array.rangeArray));
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
rangeSpinner.setAdapter(adapter);
rangeSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView, View view,
int position, long id) {
posit=position;
Log.w("isFirstTime onCreate onItemSelected","????");
if (isFirstTime) {
isFirstTime = false;
} else {
if (position == 0) {
languageToLoad = "en";
} else if (position == 1) {
languageToLoad = "fr";
} else if (position == 2) {
languageToLoad = "ja";
}
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
onConfigurationChanged(config);
}
}
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
getBaseContext().getResources().updateConfiguration(newConfig,
getBaseContext().getResources().getDisplayMetrics());
setContentView(R.layout.main);
rangeSpinner = (Spinner) findViewById(R.id.spinnerLanguage);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, getResources()
.getStringArray(R.array.rangeArray));
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
rangeSpinner.setSelection(posit);
rangeSpinner.setAdapter(adapter);
Log.w("isFirstTime onConfigurationChanged","????");
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.w("isFirstTime onResume","????");
}
}