registerForContextMenu:
之间的区别是什么注册要为给定视图显示的上下文菜单(多个视图可以显示上下文菜单)。此方法将视图上的View.OnCreateContextMenuListener设置为此活动
调用registerForContextMenu()并将其传递给您想要提供上下文菜单的视图。 当此视图收到长按时,会显示上下文菜单。
和setOnCreateContextMenuListener:
在构建此视图的上下文菜单时注册要调用的回调。 如果此视图不能长时间点击,则可以长时间点击。
使用哪一个?关于长期可点击的东西:两者都在做同样的事情......
由于
答案 0 :(得分:18)
如有疑问......请查看Android源代码!毕竟它是开源的。 :)
<强> GIT中://android.git.kernel.org/platform/frameworks/base.git/core/java/android/view/View.java:强>
/**
* Register a callback to be invoked when the context menu for this view is
* being built. If this view is not long clickable, it becomes long clickable.
*
* @param l The callback that will run
*
*/
public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
if (!isLongClickable()) {
setLongClickable(true);
}
mOnCreateContextMenuListener = l;
}
<强> GIT中://android.git.kernel.org/platform/frameworks/base.git/core/java/android/app/Activity.java:强>
/**
* Registers a context menu to be shown for the given view (multiple views
* can show the context menu). This method will set the
* {@link OnCreateContextMenuListener} on the view to this activity, so
* {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} will be
* called when it is time to show the context menu.
*
* @see #unregisterForContextMenu(View)
* @param view The view that should show a context menu.
*/
public void registerForContextMenu(View view) {
view.setOnCreateContextMenuListener(this);
}
所以,答案是他们是一样的。 registerForContextMenu()
除了调用setOnCreateContextMenuListener()
之外什么都不做。