【发布时间】:2018-10-25 14:56:35
【问题描述】:
我正在尝试使用区域设置更改应用程序中的语言。我在RadioGroup 中有两个radioButton。当我尝试更改语言时,setOnCheckedChangeListener 继续呼叫。如果我删除语言环境代码,它工作正常
Locale locale;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.toggle);
Toast.makeText(this, ""+getString(R.string.app_name), Toast.LENGTH_SHORT).show();
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton rb = (RadioButton) group.findViewById(checkedId);
Toast.makeText(MainActivity.this, "Inside", Toast.LENGTH_SHORT).show();
if (rb!=null){
switch(checkedId) {
case R.id.rdEnglish:
// do operations specific to this selection
locale = new Locale("en");
rb.setChecked(true);
setLocale();
break;
case R.id.rdTamil:
// do operations specific to this selection
locale = new Locale("ta");
setLocale();
break;
default:
locale = new Locale("en");
setLocale();
}
}
}
});
}
public void setLocale() {
Locale.setDefault(locale);
Configuration configuration = new Configuration();
configuration.locale = locale;
getBaseContext().getResources().updateConfiguration(configuration,getBaseContext().getResources().getDisplayMetrics());
recreate();
}
}
【问题讨论】:
标签: android radio-button locale radio-group