3

我想根据下拉菜单列表更改语言。但是此代码无法正常工作。它显示了这个问题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","????");

}
}
4

4 回答 4

1

只是为了尝试...
而不是调用onConfigChange()方法...让一个用户定义函数说toChangeConfig(Config c)并在该函数中执行与您在该函数中编写的代码相同的步骤,onConfigChange()然后在onItemSelected.

于 2012-09-19T12:35:25.127 回答
0

为布局中的根元素指定一个 ID。现在尝试在第二次膨胀内容之前删除所有视图。

这只是一个猜测,但值得一试。

于 2012-09-15T16:22:43.860 回答
0

尝试使用 updateConfiguration 方法

DisplayMetrics dm = getResources().getDisplayMetrics();
updateConfiguration(config, dm);
于 2012-09-18T11:10:38.150 回答
0

将字符串外部化,使用命名方法,让 android 为您处理。

您还需要按照 MKJParekh 所说的去做 - onConfigChange() 是一个回调,而不是您调用的东西。当你切换配置时它会被调用,所以放另一个

Log.d("MY APP", "::onConfigurationChange() fired!");

并等待它出现。此外,该代码不应在 onCreate() 中,而应在 onResume 中 - 请记住 onCreate() 需要是一个轻量级方法。

于 2012-09-19T14:30:22.567 回答