如何捕获对菜单项的长按?

发布于 2024-12-07 18:10:19 字数 602 浏览 0 评论 0原文

我有一个典型的菜单,我想为其中一项设置一个 onLongClickListener 。换句话说,我希望该项目执行正常的 onOptionsItemSelected 功能以及长按功能。

    MenuItem item;
    item = menu.findItem(android.R.id.home);

item.setOnLongClickListener(new OnLongClickListener() {
        public boolean onLongClick(View v) {
            Context context = getApplicationContext();
            CharSequence text = "Long Press";
            int duration = Toast.LENGTH_SHORT;

            Toast toast = Toast.makeText(context, text, duration);
            toast.show();
            return true;
        }

    });

I have a typical menu and I'm wanting to set a onLongClickListener for one of the items. In other words, I want this item to perform it's normal onOptionsItemSelected function, as well as, a long press function.

    MenuItem item;
    item = menu.findItem(android.R.id.home);

item.setOnLongClickListener(new OnLongClickListener() {
        public boolean onLongClick(View v) {
            Context context = getApplicationContext();
            CharSequence text = "Long Press";
            int duration = Toast.LENGTH_SHORT;

            Toast toast = Toast.makeText(context, text, duration);
            toast.show();
            return true;
        }

    });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱本泡沫多脆弱 2024-12-14 18:10:19

我只需在 menuItem 上使用 setActionView 即可做到这一点。如果有帮助,您可以按照此过程进行操作。

    for(int i = 0; i < menu.size(); i++){
        View v = new View(this);
        v.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                //Your longclick listener callback logic goes here
                return false;
            }

        });
        menu.getItem(i).setActionView(v);
    }

I was able to do this by simply using setActionView on menuItem. You can follow this procedure if it helps.

    for(int i = 0; i < menu.size(); i++){
        View v = new View(this);
        v.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                //Your longclick listener callback logic goes here
                return false;
            }

        });
        menu.getItem(i).setActionView(v);
    }
雨的味道风的声音 2024-12-14 18:10:19

使用 Menu 上的 findItem 方法获取视图,并在每个视图上设置长按监听器。

Use the findItem method on Menu to get your views, and set your long click listener on each view.

梦萦几度 2024-12-14 18:10:19

这种方法是不对的,它会扰乱整个流程,但你可以这样做:

    private interface OnMenuItemLongClickListener{
        boolean onMenuItemLongClik(MenuItem m);
    }
    private void getMenuItemsView(Activity a, final Menu m, final OnMenuItemLongClickListener listener) throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException{
        View homeButton = a.findViewById(android.R.id.home);
        ViewParent parentOfHome = homeButton.getParent().getParent(); //ActionBarView is parent of home ImageView, see layout file in sources

        if (!parentOfHome.getClass().getName().contains("ActionBarView")) {
            parentOfHome = parentOfHome.getParent(); 
            Class absAbv = parentOfHome.getClass().getSuperclass(); //ActionBarView -> AbsActionBarView class
            Field actionMenuPresenterField = absAbv.getDeclaredField("mActionMenuPresenter");
            actionMenuPresenterField.setAccessible(true);
            Object actionMenuPresenter = actionMenuPresenterField.get(parentOfHome);
            Field actionMenuViewField = actionMenuPresenter.getClass().getSuperclass().getDeclaredField("mMenuView");
            actionMenuViewField.setAccessible(true);
            Object actionMenuView = actionMenuViewField.get(actionMenuPresenter);
            Field childrenField= actionMenuView.getClass().getSuperclass().getSuperclass().getDeclaredField("mChildren");
            childrenField.setAccessible(true);
            Field menuField =actionMenuPresenter.getClass().getSuperclass().getDeclaredField("mMenu");
            menuField.setAccessible(true);
            Object menu = menuField.get(actionMenuPresenter);
            Object[] menuItemsAsViews = (Object[])childrenField.get(actionMenuView);
            View.OnLongClickListener longListener = new View.OnLongClickListener() {

                @Override
                public boolean onLongClick(View v) {

                    return listener.onMenuItemLongClik(m.findItem(v.getId()));
                }
            };
            for(Object menuView:menuItemsAsViews ){
                View v = (View)menuView;
                v.setOnLongClickListener(longListener);
            }


    }
 }

用法:

@Override
public boolean onPrepareOptionsMenu(final Menu menu) {
        new Handler().postDelayed(new Runnable(){

            @Override
            public void run() {
            getMenuItemsViews(MainActivity.this, menu);
            }}, 100); //must be called after the menu views are generated, otherwise NPE.
    return super.onPrepareOptionsMenu(menu);
}

菜单项在溢出不会被考虑。

This approach is not right, it disturbs entire flow, but here you go:

    private interface OnMenuItemLongClickListener{
        boolean onMenuItemLongClik(MenuItem m);
    }
    private void getMenuItemsView(Activity a, final Menu m, final OnMenuItemLongClickListener listener) throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException{
        View homeButton = a.findViewById(android.R.id.home);
        ViewParent parentOfHome = homeButton.getParent().getParent(); //ActionBarView is parent of home ImageView, see layout file in sources

        if (!parentOfHome.getClass().getName().contains("ActionBarView")) {
            parentOfHome = parentOfHome.getParent(); 
            Class absAbv = parentOfHome.getClass().getSuperclass(); //ActionBarView -> AbsActionBarView class
            Field actionMenuPresenterField = absAbv.getDeclaredField("mActionMenuPresenter");
            actionMenuPresenterField.setAccessible(true);
            Object actionMenuPresenter = actionMenuPresenterField.get(parentOfHome);
            Field actionMenuViewField = actionMenuPresenter.getClass().getSuperclass().getDeclaredField("mMenuView");
            actionMenuViewField.setAccessible(true);
            Object actionMenuView = actionMenuViewField.get(actionMenuPresenter);
            Field childrenField= actionMenuView.getClass().getSuperclass().getSuperclass().getDeclaredField("mChildren");
            childrenField.setAccessible(true);
            Field menuField =actionMenuPresenter.getClass().getSuperclass().getDeclaredField("mMenu");
            menuField.setAccessible(true);
            Object menu = menuField.get(actionMenuPresenter);
            Object[] menuItemsAsViews = (Object[])childrenField.get(actionMenuView);
            View.OnLongClickListener longListener = new View.OnLongClickListener() {

                @Override
                public boolean onLongClick(View v) {

                    return listener.onMenuItemLongClik(m.findItem(v.getId()));
                }
            };
            for(Object menuView:menuItemsAsViews ){
                View v = (View)menuView;
                v.setOnLongClickListener(longListener);
            }


    }
 }

Usage:

@Override
public boolean onPrepareOptionsMenu(final Menu menu) {
        new Handler().postDelayed(new Runnable(){

            @Override
            public void run() {
            getMenuItemsViews(MainActivity.this, menu);
            }}, 100); //must be called after the menu views are generated, otherwise NPE.
    return super.onPrepareOptionsMenu(menu);
}

MenuItem's in the overflow WILL NOT be considered.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文