本文介绍了如何prevent弹出菜单中从收盘点击复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索净了很多,但没有任何关于preventing弹出菜单中的关闭。

每当我在复选框项或任何其他弹出式菜单项,单击弹出菜单中解雇自己。我怎样才能从弹出菜单中驳回当用户选中/清除复选框prevent它。

我展示的动作条菜单项的Click事件弹出式菜单。

  // main_menu.xml
<菜单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
工具:上下文=com.worldofjobs.woj.MainActivity><项目
    机器人:ID =@ + ID / action_popUpMenu
    机器人:图标=@绘制/ ic_action_overflow
    机器人:标题=@字符串/ main_action_popUpMenu
    应用:showAsAction =总是/>&所述; /菜单>//popup_items.xml
< XML版本=1.0编码=UTF-8&GT?;
<菜单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android><项目
    机器人:ID =@ + ID / refresh_item
    机器人:标题=@字符串/ main_refresh/>
<项目
    机器人:ID =@ + ID / checkbox_item
    机器人:可检查=真
    机器人:标题=开始通知/>
<项目
    机器人:ID =@ + ID / changePasswrod_item
    机器人:标题=@字符串/ main_changePassword/>
<项目
    机器人:ID =@ + ID / deleteAccount_item
    机器人:标题=@字符串/ main_deleteAccount/>
<项目
    机器人:ID =@ + ID / logout_item
    机器人:标题=@字符串/ main_logout/>&所述; /菜单>/ **
 *显示上的点击弹出菜单操作栏菜单从膨胀
 * menu.pop_items的XML
 * /
私人无效showPopup(){    尝试{        视图V = findViewById(R.id.action_popUpMenu);        PopupMenu的弹出=新PopupMenu的(这一点,V);
        popup.setOnMenuItemClickListener(MainActivity.this);        MenuInflater吹气= popup.getMenuInflater();
        inflater.inflate(R.menu.pop_items,popup.getMenu());
        popup.show();    }赶上(例外五){        Log.e(MainActivity-showPopup:,e.toString());
    }
}/ **
 *处理弹出菜单项的单击事件
 * /
@覆盖
公共布尔onMenuItemClick(菜单项项){    super.onMenuItemSelected(1项);
    开关(item.getItemId()){    案例R.id.refresh_item:
        刷新();
        返回true;    案例R.id.checkbox_item:
        返回true;    案例R.id.changePasswrod_item:
        changePasswordPopup();
        返回true;    案例R.id.deleteAccount_item:
        删除帐户();
        返回true;    案例R.id.logout_item:
        session.logout();
        完();
        返回true;
    }
    返回true;
}


解决方案

这里的窍门是要表明解聘后右侧的菜单。

下面是一个示例code片断:

  popupMenu.setOnMenuItemClickListener(新OnMenuItemClickListener(){
@覆盖
公共布尔onMenuItemClick(菜单项项){
    如果(item.getItemId()== R.id.search_by_date_checkbox){
        item.setChecked(item.isChecked()!);
    }
    //这是这里的窍门!!!!
    popupMenu.show();
    返回true;
    }});

您可以试试这招你的code!这是我做的。 :)

I search a lot on net but there is nothing about preventing popup menu from closing.

Whenever i click on checkbox item or any other popup menu item, popup menu dismiss itself. How can i prevent it from dismissing when user check/uncheck checkbox in popup menu.

I'm showing popup menu on actionbar-menu item's click event.

//main_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.worldofjobs.woj.MainActivity" >

<item
    android:id="@+id/action_popUpMenu"
    android:icon="@drawable/ic_action_overflow"
    android:title="@string/main_action_popUpMenu"
    app:showAsAction="always"/>

</menu>

//popup_items.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item
    android:id="@+id/refresh_item"
    android:title="@string/main_refresh"/>
<item
    android:id="@+id/checkbox_item"
    android:checkable="true"
    android:title="Start notification"/>
<item
    android:id="@+id/changePasswrod_item"
    android:title="@string/main_changePassword"/>
<item
    android:id="@+id/deleteAccount_item"
    android:title="@string/main_deleteAccount"/>
<item
    android:id="@+id/logout_item"
    android:title="@string/main_logout"/>

</menu>

/**
 * Shows popup menu on click of action bar-menu inflates from
 * menu.pop_items-xml
 */
private void showPopup() {

    try {

        View v = findViewById(R.id.action_popUpMenu);

        PopupMenu popup = new PopupMenu(this, v);
        popup.setOnMenuItemClickListener(MainActivity.this);

        MenuInflater inflater = popup.getMenuInflater();
        inflater.inflate(R.menu.pop_items, popup.getMenu());
        popup.show();

    } catch (Exception e) {

        Log.e("MainActivity-showPopup:", e.toString());
    }
}

/**
 * Handles click events of popup menu items
 */
@Override
public boolean onMenuItemClick(MenuItem item) {

    super.onMenuItemSelected(1, item);
    switch (item.getItemId()) {

    case R.id.refresh_item:
        refresh();
        return true;

    case R.id.checkbox_item:
        return true;

    case R.id.changePasswrod_item:
        changePasswordPopup();
        return true;

    case R.id.deleteAccount_item:
        deleteAccount();
        return true;

    case R.id.logout_item:
        session.logout();
        finish();
        return true;
    }
    return true;
}
解决方案

The trick here is to show the menu right after it dismisses.
Below is a sample code snippet:

popupMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
    if(item.getItemId()==R.id.search_by_date_checkbox){
        item.setChecked(!item.isChecked());
    }
    //This is the trick here!!!!
    popupMenu.show();
    return true;
    }

});

You can try this trick with your code! This is how I did it. :)

这篇关于如何prevent弹出菜单中从收盘点击复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 22:34