Android ActionBar选项长按事件

4

Android有关ActionBar项选项长按的内容,我希望在ActionBar选项上长按时显示文本,就像长按ActionBar时显示提示一样。

6个回答

10

你想在操作栏上捕获菜单项的长按吗?对我来说,在找了2,3个小时后,我找到了这个解决方案。这对我来说完美地起作用。

      @Override
     public boolean onCreateOptionsMenu(final Menu menu) {



    getMenuInflater().inflate(R.menu.menu, menu);

    new Handler().post(new Runnable() {
        @Override
        public void run() {
            final View v = findViewById(R.id.action_settings);

            if (v != null) {
                v.setOnLongClickListener(new CustomLongOnClickListener());
            }
        }
    });

    return true;
}

但第一次它运行良好。第二次当我们点击菜单项时,它显示相同的空toast消息。 - Pinki
1
@YeeKhin。非常感谢。 - user4813855
但是findviewbyid总是返回null,为什么? - hyyou2010

6
对于较新版本的Android,以下方法对我来说效果很好 - 我已经在Android 4.2和Android 5.0.1上测试过。
我的想法是用自定义视图替换操作图标视图。 在这里,我必须处理单击事件,并且可以处理长按事件。
如果我希望外观与普通操作栏图标完全相同,则可以使用以下方法。
首先,创建一个只包含带有您图标的ImageButton的布局。
<?xml version="1.0" encoding="utf-8"?>
<ImageButton xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/myButton"
    style="?android:attr/actionButtonStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:contentDescription="@layout/text_view_initializing"
    android:src="@drawable/ic_action_plus" />

然后将此ImageButton放入操作栏并附加监听器。
MenuItem myItem = menu.findItem(R.id.my_action);
myItem.setActionView(R.layout.my_image_button);
myItem.getActionView().setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(final View v) {
        // here, I have to put the stuff that normally goes in onOptionItemSelected 
    }
});
myItem.getActionView().setOnLongClickListener(new OnLongClickListener() {
    @Override
    public boolean onLongClick(final View v) {
        // here, I put the long click stuff
    }
});

重要说明:这只适用于项目出现在操作栏中的情况。因此,如果选项出现在菜单下拉列表中,则无法通过此方式进行长按操作。


5

user1206890,您不需要监听长按事件。如果您想显示操作提示,只需在菜单中设置标题即可。2.3和4.0版本已经验证。


4
如果您通过 android:actionLayout 创建自己的操作视图,则可以为自己的小部件设置长按事件监听器。您无法访问未由自己创建的操作栏中的小部件。

然后根据https://dev59.com/rHvZa4cB1Zd3GeqP_B24#21026866的建议,考虑Toast的大小,把它放在想要的位置,这很有帮助。 - jayeffkay

1

我认为"findViewById"是最简单的查找方法。
只需执行

View action_example = findViewById(R.id.action_example);
        if(action_example!=null)action_example.setOnLongClickListener(
                new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        Toast.makeText(MainActivity.this, "action_example", Toast.LENGTH_SHORT).show();
                        return true;
                    }
                }
        );

1

以下是与我一起使用的函数代码,感谢@YeeKhin 将“main”更改为您的菜单名称,“action_refresh”更改为您的操作名称,“Activity”更改为您的活动名称

public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.main, menu);

        new Handler().post(new Runnable() {
            @Override
            public void run() {
                final View v = findViewById(R.id.action_refresh);

                if (v != null) {
                    v.setOnLongClickListener(new View.OnLongClickListener() {
                        @Override
                        public boolean onLongClick(View v) {
                            Toast.makeText(Activity.this,"Long Press!!",Toast.LENGTH_LONG).show();
                            return false;
                        }
                    });
                }
            }
        });

        return true;
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接