在有人提出更好的“一次通话”答案之前,这是我在问题中提到的自定义解决方法实现,在 Sam 的提示的帮助下,以防有人需要相同的功能:
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
if (showingMenu) {
// The menu button was clicked or the hard keyboard was
// slid open/closed while the menu was already showing
return true;
}
// Otherwise, either the menu was clicked or openOptionsMenu() was called
if (codeEntered) {
// Code was entered and then openOptionsMenu() was called
showingMenu = true;
// Menu will now be shown
return true;
} else {
// The menu button was clicked, ask for code
askForCode();
// Don't show menu yet
return false;
}
}
@Override
public void onOptionsMenuClosed(Menu menu) {
showingMenu = false;
codeEntered = false;
}
private void askForCode() {
codeEntered = getUserInput();
if (codeEntered)
openOptionsMenu();
}
getUserInput()
AlertDialog
实际上是在 an和 anEditText
的帮助下发生的,TextWatcher
但实施细节超出了这个问题的范围,除非有人感兴趣。