2012-08-31 86 views
3

我想更改語言基礎上的下拉菜單list.But此代碼無法正常工作。它顯示出這個問題Window already focused, ignoring focus gain of: [email protected].窗口已經聚焦,忽略焦點收益:[email protected]

問題在哪裏我找不出來。請任何人幫助我。

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

給佈局的根元素一個ID。現在嘗試在第二次對內容進行膨脹之前刪除所有視圖。

這只是一個猜測,但值得一試。

0

嘗試使用updateConfiguration方法

DisplayMetrics dm = getResources().getDisplayMetrics(); 
updateConfiguration(config, dm); 
1

只是爲了試試...
而不是調用onConfigChange()方法......讓一個用戶定義函數說toChangeConfig(Config c)和做的代碼相同的步驟在功能你在onConfigChange()中寫道,現在在onItemSelected中調用這個函數。

0

使字符串外部化,使用命名方法並讓android爲您處理它。

您還需要做MKJParekh所說的 - onConfigChange()是一個回調函數,而不是您調用的函數。它會在您切換配置時被調用,因此請換另一個

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

並等待它顯示出來。此外,該代碼不應該在onCreate()中,而應該放在onResume中 - 請記住,onCreate()需要是一個輕量級的方法。

相關問題