我想在下拉菜单列表中更改语言基础。但此代码无法正常工作。它显示了此问题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","????");
}
}
答案 0 :(得分:1)
只是为了尝试......
而不是调用onConfigChange()
方法...让一个用户定义函数说toChangeConfig(Config c)
并在你写的那个函数中执行相同的代码步骤在onConfigChange()
中,现在在onItemSelected
中调用此函数。
答案 1 :(得分:0)
为布局中的根元素指定ID。现在尝试在第二次膨胀内容之前删除AllViews。
这只是猜测,但值得一试。
答案 2 :(得分:0)
尝试使用updateConfiguration方法
DisplayMetrics dm = getResources().getDisplayMetrics();
updateConfiguration(config, dm);
答案 3 :(得分:0)
外化字符串,使用命名方法让android为你处理它。
你还需要做MKJParekh所说的 - onConfigChange()是一个回调,而不是你所说的。切换配置时会调用它,所以再放一个
Log.d("MY APP", "::onConfigurationChange() fired!");
等待它出现。此外,该代码不应该在onCreate()中,而应该在onResume中 - 请记住onCreate()需要是一个轻量级方法。