我在一个对话框中从Imagview打开了一个popupmenu。当点击菜单项时,我有一个文本到语音,从10到1说倒计时。我想在点击菜单项后关闭菜单。我试过popup.dismiss()和closeOptionsMenu()但没有运气。请帮帮我。
Imageview counter = (Imageview) findViewById(R.id.imgview);
counter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
PopupMenu countMenu = new PopupMenu(MainActivity.this,counter);
countMenu.getMenuInflater().inflate(R.menu.counter_menu,
countMenu.getMenu());
countMenu.setOnMenuItemClickListener(new
PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(final MenuItem menuItem) {
String selectedCount = menuItem.getTitle().toString();
countMenu.dismiss();
//Tried closeOptionsMenu(); too here but doesn't work
if(selectedCount.equals("Some String")
{
//TTS counter from 10 to 0
return true;
}
countMenu.show();
返回true将在if语句之外。您已将return语句放在if语句中,这就是它在计数完成后关闭的原因。
示例代码:
countMenu.dismiss();
return true;
if(selectedCount.equals("Some String")
{
//TTS counter from 10 to 0
}