2017-03-01 58 views
3

我創造了ENGLSIH多語言應用和印地文Android的語言環境和updateConfiguration已過時

下面的代碼,我用其完美的工作,但有些東西是從它過時了,只是告訴我該怎麼解決呢,

棄用代碼是

conf.locale = myLocale; 
res.updateConfiguration(conf, dm); 

我的整個代碼(這是不完整的代碼我只是顯示你的主要組成部分)

private Intent intent; 
    private Locale myLocale; 
    private Resources res; 
    private DisplayMetrics dm; 
    private Configuration conf; 

switch(lang) 
        { 
         case "no_lang": 

          editor = preferences.edit(); 
          editor.putString("set_language","en"); 
          editor.commit(); 

          myLocale = new Locale("en"); 
          res = getResources(); 
          dm = res.getDisplayMetrics(); 
          conf = res.getConfiguration(); 
          conf.locale = myLocale; 
          res.updateConfiguration(conf, dm); 
          intent = new Intent(SplashActivity.this, MainActivity.class); 
          startActivity(intent); 
          finish(); 
          break; 
         case "en": 

          editor = preferences.edit(); 
          editor.putString("set_language","en"); 
          editor.commit(); 

          myLocale = new Locale("en"); 
          res = getResources(); 
          dm = res.getDisplayMetrics(); 
          conf = res.getConfiguration(); 
          conf.locale = myLocale; 
          res.updateConfiguration(conf, dm); 
          intent = new Intent(SplashActivity.this, MainActivity.class); 
          startActivity(intent); 
          finish(); 
          break; 
         case "hi": 
          editor = preferences.edit(); 
          editor.putString("set_language","hi"); 
          editor.commit(); 

          myLocale = new Locale("hi"); 
          res = getResources(); 
          dm = res.getDisplayMetrics(); 
          conf = res.getConfiguration(); 
          conf.locale = myLocale; 
          res.updateConfiguration(conf, dm); 
          intent = new Intent(SplashActivity.this, MainActivity.class); 
          startActivity(intent); 
          finish(); 
          break; 
        } 

我遵循這些鏈接,但我沒有得到它... link1link2

回答

1

確定我使用@Bassel Mourjan

我有一個基本活動和this answer解決它剩下的都是碎片,下面是我的代碼 (我在android 7.1.1(genymotion emu)中測試了這個代碼,它起作用)

請參閱(使用)MyContextWrapper類從上面的鏈接。

在我的按鈕MainActivity

單擊我是從該活動我得到用戶如選擇的語言打開活動的結果是:英語,印地文,馬拉

MainActivity:

 // i use shared preference to save my language so that when user close 
     //app and open it, then his previous language will be set 
      @Override 
      public void attachBaseContext(Context newBase) 
      { 
       SharedPreferences preferences1 = newBase.getSharedPreferences("global",newBase.MODE_PRIVATE); 
       String lang=preferences1.getString("set_language","en"); // at start no language selected so default is english 
       super.attachBaseContext(MyContextWrapper.wrap(newBase,lang)); 
      } 



    @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 

      navigation_header_language.setOnClickListener(this);//Language button 
    } 

     @Override 
     public void onClick(View v) 
     { 

      switch (v.getId()) 
      { 
       case R.id.navigation_header_language: 

        Intent i = new Intent(this, LanguageActivity.class); 
        startActivityForResult(i, 101); 

        break; 
      } 
     } 

// when user selects language i store it in shared preference and set result 
// as RESULT_OK and here in onActivityResult's result_ok i simply recreate activity 
//so that attachBaseContext called again and language set as we want 
     @Override 
     protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
      super.onActivityResult(requestCode, resultCode, data); 
      if (requestCode == 101) { 
       if(resultCode == Activity.RESULT_OK) 
       { 
        this.recreate(); 
       } 

       if (resultCode == Activity.RESULT_CANCELED) { 
        //Write your code if there's no result 
       } 
      } 
     } 

這裏是LanguageActivity這是我從主要活動呼叫結果

 public class LanguageActivity extends AppCompatActivity 
     { 
      private CardView english_language,hindi_language,marathi_language; 
      private SharedPreferences preferences; 
      private SharedPreferences.Editor editor; 

      @Override 
      protected void onCreate(Bundle savedInstanceState) 
      { 
       super.onCreate(savedInstanceState); 
       setContentView(R.layout.fragment_language); 

       english_language=(CardView)findViewById(R.id.english_language); 
       hindi_language=(CardView)findViewById(R.id.hindi_language); 
       marathi_language=(CardView)findViewById(R.id.marathi_language); 

       preferences =getSharedPreferences("global",MODE_PRIVATE); 

       english_language.setOnClickListener(new View.OnClickListener() 
       { 
        @Override 
        public void onClick(View v) 
        { 
         editor = preferences.edit(); 
         editor.putString("set_language","en"); 
         editor.commit(); 
         setResultCode(); 
        } 
       }); 

       hindi_language.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) 
        { 
         editor = preferences.edit(); 
         editor.putString("set_language","hi"); 
         editor.commit(); 
         setResultCode(); 
        } 
       }); 

       marathi_language.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) 
        { 
         editor = preferences.edit(); 
         editor.putString("set_language","mr"); 
         editor.commit(); 
         setResultCode(); 
        } 
       }); 
      } 

      private void setResultCode() 
      { 
       Intent returnIntent = new Intent(); 
       setResult(Activity.RESULT_OK,returnIntent); 
       finish(); 
// when this activity close, onActivityResult method call from Mainactivity 
//and there i used this.recreate() which will recreate mMainactivity an 
//attachBaseContext will be called once again 
      } 
     }