2017-02-14 2 views
0

ボタンをクリックしてアプリケーション言語の設定を変更する場合にのみ保護されたメソッドを呼び出します。私はアンドロイドとjavaに新しいので、私は道を見出すことができませんでした。前もって感謝します。あなたが唯一のメソッドの呼び出しを追加する必要がボタンをクリックした後に保護されたメソッドをロードする

public class MyContextWrapper extends ContextThemeWrapper { 

public MyContextWrapper(Context base) { 
    super(base,R.style.AppTheme); 
} 

@SuppressWarnings("deprecation") 
public static ContextWrapper wrap(Context context, String language) { 
    Configuration config = context.getResources().getConfiguration(); 
    Locale sysLocale = null; 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { 
     sysLocale = getSystemLocale(config); 
    } else { 
     sysLocale = getSystemLocaleLegacy(config); 
    } 
    if (!language.equals("") && !sysLocale.getLanguage().equals(language)) { 
     Locale locale = new Locale(language); 
     Locale.setDefault(locale); 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { 
      setSystemLocale(config, locale); 
     } else { 
      setSystemLocaleLegacy(config, locale); 
     } 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { 
      context = context.createConfigurationContext(config); 
     } else { 
      context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics()); 
     } 
    } 
    return new MyContextWrapper(context); 
} 

@SuppressWarnings("deprecation") 
public static Locale getSystemLocaleLegacy(Configuration config){ 
    return config.locale; 
} 

@TargetApi(Build.VERSION_CODES.N) 
public static Locale getSystemLocale(Configuration config){ 
    return config.getLocales().get(0); 
} 

@SuppressWarnings("deprecation") 
public static void setSystemLocaleLegacy(Configuration config, Locale locale){ 
    config.locale = locale; 
} 

@TargetApi(Build.VERSION_CODES.N) 
public static void setSystemLocale(Configuration config, Locale locale){ 
    config.setLocale(locale); 
} 
} 

答えて

0

は、次のように

@Overide 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_choose_language); 
    btnAss = (RadioButton) findViewById(R.id.radioButtonAss); 
    btnEng = (RadioButton) findViewById(R.id.radioButtonEng); 
    btnSubmit = (Button) findViewById(R.id.btnSelectLanguage); 
    btnSubmit.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      if (btnAss.isChecked()) { 


       //here it should call the protected method attachBaseContext 
       // attachBaseContext(getApplicationContext()); 
       Intent intent = new Intent(getApplicationContext(), WelcomeActivity.class); 
       startActivity(intent); 
       finish(); 
      } else { 
       Intent intent = new Intent(getApplicationContext(), WelcomeActivity.class); 
       startActivity(intent); 
       finish(); 
      } 
     } 
    }); 


} 
@Override 
protected void attachBaseContext(Context newBase) { 
    super.attachBaseContext(MyContextWrapper.wrap(newBase,"as")); 
} 

ContextWrapperです。

@Override 
     public void onClick(View view) { 
      if (btnAss.isChecked()) { 

      //here it should call the protected method attachBaseContext 
      attachBaseContext(getApplicationContext()); 
      Intent intent = new Intent(getApplicationContext(), WelcomeActivity.class); 
      startActivity(intent); 
      finish(); 
     } else { 
      Intent intent = new Intent(getApplicationContext(), WelcomeActivity.class); 
      startActivity(intent); 
      finish(); 
     } 

しかし、私は何がやりたいことは、一度アプリが実行されていることを変更することだと思い、そのrigthのですか?

+0

はい、アクティビティが読み込まれたときにattachBaseContextメソッドが呼び出されますが、ボタンがクリックされた後でなければなりません。 – dhruv

関連する問題