这是准备代码:
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.language_menu, menu);
MenuItem menuItem = menu.findItem(R.id.arabic);
if (UtilityPreferenceManager.getSelectedLanguage() == UtilityPreferenceManager.LanguageArabic) {
menuItem.setChecked(true);
} else {
menuItem.setChecked(false);
}
menuItem = menu.findItem(R.id.english);
if (UtilityPreferenceManager.getSelectedLanguage() == UtilityPreferenceManager.LanguageEnglish) {
menuItem.setChecked(true);
} else {
menuItem.setChecked(false);
}
}
@Override
public boolean onContextItemSelected(MenuItem item) {
item.setChecked(true);
switch (item.getItemId()) {
case R.id.arabic:
if (UtilityPreferenceManager.getSelectedLanguage() != UtilityPreferenceManager.LanguageArabic) UtilityPreferenceManager.changeLanguage();
return true;
case R.id.english:
if (UtilityPreferenceManager.getSelectedLanguage() != UtilityPreferenceManager.LanguageEnglish) UtilityPreferenceManager.changeLanguage();
return true;
default:
return super.onContextItemSelected(item);
}
}
,这是菜单xml:
<group android:checkableBehavior="single">
<item android:id="@+id/arabic"
android:title="@string/arabic" />
<item android:id="@+id/english"
android:title="@string/english" />
</group>
问题是菜单始终显示为选择了英语。我确信语言首选项在首选项中正确保存。事实上,if条件工作正常,但似乎在完成onCreateContextMenu