2011-03-22 25 views
-1

我在android中製作了一個多語言應用程序。在我的語言選擇活動中,我讓用戶選擇他們的偏好語言。當用戶選擇我改變使用以下代碼的應用程序的配置:如何在android中獲取活動語言環境?

Locale locale = new Locale(lang); 
    Locale.setDefault(locale); 
    Configuration config = new Configuration(); 
    config.locale = locale; 
    getBaseContext().getResources().updateConfiguration(config, 
      getBaseContext().getResources().getDisplayMetrics()); 

現在載入一個活動我想要查詢的用戶是否已經改變的區域設置或者是相同的之前。 我想獲取活動的語言環境。我知道如何讓應用程序地區...

回答

1

首先添加以下到您的活動代碼在AndroidManifest.xml

​​

以上將使應用程序在語言和區域設置的配置更改進行更新。

在Activity類中使用以下java編碼,最好在onResume()和onCreate()方法中使用。

public class localeExample extends Activity { 
      Locale l; 

      /** Called when the activity is first created. */ 
      @Override 
      public void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState); 
       l=new Locale(); 
       l=Locale.getDefault(); 
      } 

      /** Called when the activity is resumed. */ 
      @Override 
      protected void onResume(){ 
       super.onResume(); 
       l=new Locale(); 
       l=Locale.getDefault(); 
      } 
} 
+0

一旦我有地方我該怎麼處理它?我不需要添加到我的配置文件?我正在談論onResume()方法.... – user590849 2011-03-22 11:23:26

+0

你想要做什麼與區域設置取決於你..你問如何獲得活動的區域設置,這就是答案。這將從手機中讀取默認語言環境 – 2011-06-03 05:05:35

相關問題