如何捕获对菜单项的长按?
我有一个典型的菜单,我想为其中一项设置一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我只需在 menuItem 上使用 setActionView 即可做到这一点。如果有帮助,您可以按照此过程进行操作。
I was able to do this by simply using setActionView on menuItem. You can follow this procedure if it helps.
使用
Menu
上的findItem
方法获取视图,并在每个视图上设置长按监听器。Use the
findItem
method onMenu
to get your views, and set your long click listener on each view.这种方法是不对的,它会扰乱整个流程,但你可以这样做:
用法:
菜单项在溢出
不会
被考虑。This approach is not right, it disturbs entire flow, but here you go:
Usage:
MenuItem's in the overflow
WILL NOT
be considered.