常见的长按事件

Android学习笔记长按事件的处理-LMLPHP

代码示例:

  @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//第二步:把长按事件注册到菜单,并打开菜单
ImageView imageView = findViewById(R.id.imageView);
imageView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
registerForContextMenu(v);
openContextMenu(v);
return false;
}
});
}
//第一步:在MainActivity中重写onCreateContextMenu菜单,为菜单添加选项值
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add("收藏");
menu.add("下载");
}
05-11 20:13