2011-12-28 5 views
6

¿cuál es la diferencia entre registerForContextMenu:diferencia registerForContextMenu y setOnCreateContextMenuListener?

Registra el menú contextual que se muestra para la vista dada (múltiples vistas pueden mostrar el menú contextual). Este método configurará View.OnCreateContextMenuListener en la vista para esta actividad

Llamar registerForContextMenu() y pasarle la Vista que desea dar a un menú contextual. Cuando esta vista recibe una pulsación larga, muestra un menú contextual.

y setOnCreateContextMenuListener:

Registrar una devolución de llamada que se invoca cuando el menú de contexto para este punto de vista se está construyendo. Si esta vista no se puede hacer clic en, se puede hacer clic mucho.

¿cuál usar? y sobre la larga puede hacer clic cosas: ambos están haciendo lo mismo ...

Gracias

+0

Parece que son lo mismo. –

Respuesta

17

En caso de duda ... mirar el código fuente de Android! Es de código abierto, después de todo. :)

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); 
} 

Por lo tanto, la respuesta es que son lo mismo. registerForContextMenu() no hace nada excepto invocar setOnCreateContextMenuListener().

+0

mmh gracias, nunca he tratado de ver el código fuente, en ningún idioma, me temo que me perderé en unos pocos segundos, pero bueno, voy a echar un vistazo la próxima vez. ¡gracias por tu respuesta! ¡Aclamaciones! – Paul

+0

respuesta a la cual no puede haber requestioning .. perfecto, @Trevor Johns. Además, muestra la forma de abordar otro tipo similar de preguntas/dudas. – Srichakradhar