仅在单击按钮更改应用程序语言配置时才调用受保护的方法。由于我是android和java的新手,我无法想出办法。提前致谢。
@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如下
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 :(得分:0)
你只需要添加对方法的调用。
@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();
}
但是,我认为你想要做的就是在应用程序运行后改变它,那是不是很有用?