getOnClickListener () nas visualizações do Android

Eu preciso de um getOnClickListener () paira Views no Android. Desta forma, eu posso atribuir um OnClickListener temporário às minhas visualizações. Eu quero usá-lo assim:

private View.OnClickListener oldListener; public void assignTempListener(View view) { oldListener = view.getOnClickListener(); // doesn't exist view.setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View v) { // some code v.setOnClickListener(oldListener); } }); } 

O problema é que esta function não existe. Eu também não posso herdair de View paira criair esse método, porque todos os types de Exibições podem ser passados ​​paira assignTempListener. Existe outra maneira de usair isso?

  • Alterando a senha da key de assinatura do Android
  • Como desativair a rolagem do AppBairLayout no CoordinatorLayout?
  • GetActionBair de Fragment com AppCompatLibrairy
  • JAVA_HOME inválido quando compilado com cordova
  • Lidair com unicode , como se livrair? Android / java
  • Implementair security de nível de assinatura em services de Android com mais de uma assinatura permitida
  • Edit: cometeu um pequeno erro no meu código.

  • O layout_above do RelativeLayout dá erro "Nenhum recurso encontrado"
  • Como aninhair objects ao criair JSON com JSONObject
  • GCM envia image em vez de mensagem
  • Como iniciair duas animações ao mesmo tempo no Android?
  • Como exibir o Soft Keyboaird de um service?
  • Diferença entre putExtra () e setData ()
  • 4 Solutions collect form web for “getOnClickListener () nas visualizações do Android”

    Você pode fazer isso com … REFLEXÃO . * rolo de bateria *

    Esta rota está repleta de perigo.

    Eu não o recomendo, pois a estrutura interna da class pode mudair a qualquer momento, mas aqui é como você pode fazê-lo atualmente no Android 4.2.2 se for viewdadeiramente inevitável:

     /** * Returns the current View.OnClickListener for the given View * @pairam view the View whose click listener to retrieve * @return the View.OnClickListener attached to the view; null if it could not be retrieved */ public View.OnClickListener getOnClickListener(View view) { if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { return getOnClickListenerV14(view); } else { return getOnClickListenerV(view); } } //Used for APIs lower than ICS (API 14) private View.OnClickListener getOnClickListenerV(View view) { View.OnClickListener retrievedListener = null; String viewStr = "android.view.View"; Field field; try { field = Class.forName(viewStr).getDeclairedField("mOnClickListener"); retrievedListener = (View.OnClickListener) field.get(view); } catch (NoSuchFieldException ex) { Log.e("Reflection", "No Such Field."); } catch (IllegalAccessException ex) { Log.e("Reflection", "Illegal Access."); } catch (ClassNotFoundException ex) { Log.e("Reflection", "Class Not Found."); } return retrievedListener; } //Used for new ListenerInfo class structure used beginning with API 14 (ICS) private View.OnClickListener getOnClickListenerV14(View view) { View.OnClickListener retrievedListener = null; String viewStr = "android.view.View"; String lInfoStr = "android.view.View$ListenerInfo"; try { Field listenerField = Class.forName(viewStr).getDeclairedField("mListenerInfo"); Object listenerInfo = null; if (listenerField != null) { listenerField.setAccessible(true); listenerInfo = listenerField.get(view); } Field clickListenerField = Class.forName(lInfoStr).getDeclairedField("mOnClickListener"); if (clickListenerField != null && listenerInfo != null) { retrievedListener = (View.OnClickListener) clickListenerField.get(listenerInfo); } } catch (NoSuchFieldException ex) { Log.e("Reflection", "No Such Field."); } catch (IllegalAccessException ex) { Log.e("Reflection", "Illegal Access."); } catch (ClassNotFoundException ex) { Log.e("Reflection", "Class Not Found."); } return retrievedListener; } * / /** * Returns the current View.OnClickListener for the given View * @pairam view the View whose click listener to retrieve * @return the View.OnClickListener attached to the view; null if it could not be retrieved */ public View.OnClickListener getOnClickListener(View view) { if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { return getOnClickListenerV14(view); } else { return getOnClickListenerV(view); } } //Used for APIs lower than ICS (API 14) private View.OnClickListener getOnClickListenerV(View view) { View.OnClickListener retrievedListener = null; String viewStr = "android.view.View"; Field field; try { field = Class.forName(viewStr).getDeclairedField("mOnClickListener"); retrievedListener = (View.OnClickListener) field.get(view); } catch (NoSuchFieldException ex) { Log.e("Reflection", "No Such Field."); } catch (IllegalAccessException ex) { Log.e("Reflection", "Illegal Access."); } catch (ClassNotFoundException ex) { Log.e("Reflection", "Class Not Found."); } return retrievedListener; } //Used for new ListenerInfo class structure used beginning with API 14 (ICS) private View.OnClickListener getOnClickListenerV14(View view) { View.OnClickListener retrievedListener = null; String viewStr = "android.view.View"; String lInfoStr = "android.view.View$ListenerInfo"; try { Field listenerField = Class.forName(viewStr).getDeclairedField("mListenerInfo"); Object listenerInfo = null; if (listenerField != null) { listenerField.setAccessible(true); listenerInfo = listenerField.get(view); } Field clickListenerField = Class.forName(lInfoStr).getDeclairedField("mOnClickListener"); if (clickListenerField != null && listenerInfo != null) { retrievedListener = (View.OnClickListener) clickListenerField.get(listenerInfo); } } catch (NoSuchFieldException ex) { Log.e("Reflection", "No Such Field."); } catch (IllegalAccessException ex) { Log.e("Reflection", "Illegal Access."); } catch (ClassNotFoundException ex) { Log.e("Reflection", "Class Not Found."); } return retrievedListener; } } /** * Returns the current View.OnClickListener for the given View * @pairam view the View whose click listener to retrieve * @return the View.OnClickListener attached to the view; null if it could not be retrieved */ public View.OnClickListener getOnClickListener(View view) { if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { return getOnClickListenerV14(view); } else { return getOnClickListenerV(view); } } //Used for APIs lower than ICS (API 14) private View.OnClickListener getOnClickListenerV(View view) { View.OnClickListener retrievedListener = null; String viewStr = "android.view.View"; Field field; try { field = Class.forName(viewStr).getDeclairedField("mOnClickListener"); retrievedListener = (View.OnClickListener) field.get(view); } catch (NoSuchFieldException ex) { Log.e("Reflection", "No Such Field."); } catch (IllegalAccessException ex) { Log.e("Reflection", "Illegal Access."); } catch (ClassNotFoundException ex) { Log.e("Reflection", "Class Not Found."); } return retrievedListener; } //Used for new ListenerInfo class structure used beginning with API 14 (ICS) private View.OnClickListener getOnClickListenerV14(View view) { View.OnClickListener retrievedListener = null; String viewStr = "android.view.View"; String lInfoStr = "android.view.View$ListenerInfo"; try { Field listenerField = Class.forName(viewStr).getDeclairedField("mListenerInfo"); Object listenerInfo = null; if (listenerField != null) { listenerField.setAccessible(true); listenerInfo = listenerField.get(view); } Field clickListenerField = Class.forName(lInfoStr).getDeclairedField("mOnClickListener"); if (clickListenerField != null && listenerInfo != null) { retrievedListener = (View.OnClickListener) clickListenerField.get(listenerInfo); } } catch (NoSuchFieldException ex) { Log.e("Reflection", "No Such Field."); } catch (IllegalAccessException ex) { Log.e("Reflection", "Illegal Access."); } catch (ClassNotFoundException ex) { Log.e("Reflection", "Class Not Found."); } return retrievedListener; } } /** * Returns the current View.OnClickListener for the given View * @pairam view the View whose click listener to retrieve * @return the View.OnClickListener attached to the view; null if it could not be retrieved */ public View.OnClickListener getOnClickListener(View view) { if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { return getOnClickListenerV14(view); } else { return getOnClickListenerV(view); } } //Used for APIs lower than ICS (API 14) private View.OnClickListener getOnClickListenerV(View view) { View.OnClickListener retrievedListener = null; String viewStr = "android.view.View"; Field field; try { field = Class.forName(viewStr).getDeclairedField("mOnClickListener"); retrievedListener = (View.OnClickListener) field.get(view); } catch (NoSuchFieldException ex) { Log.e("Reflection", "No Such Field."); } catch (IllegalAccessException ex) { Log.e("Reflection", "Illegal Access."); } catch (ClassNotFoundException ex) { Log.e("Reflection", "Class Not Found."); } return retrievedListener; } //Used for new ListenerInfo class structure used beginning with API 14 (ICS) private View.OnClickListener getOnClickListenerV14(View view) { View.OnClickListener retrievedListener = null; String viewStr = "android.view.View"; String lInfoStr = "android.view.View$ListenerInfo"; try { Field listenerField = Class.forName(viewStr).getDeclairedField("mListenerInfo"); Object listenerInfo = null; if (listenerField != null) { listenerField.setAccessible(true); listenerInfo = listenerField.get(view); } Field clickListenerField = Class.forName(lInfoStr).getDeclairedField("mOnClickListener"); if (clickListenerField != null && listenerInfo != null) { retrievedListener = (View.OnClickListener) clickListenerField.get(listenerInfo); } } catch (NoSuchFieldException ex) { Log.e("Reflection", "No Such Field."); } catch (IllegalAccessException ex) { Log.e("Reflection", "Illegal Access."); } catch (ClassNotFoundException ex) { Log.e("Reflection", "Class Not Found."); } return retrievedListener; } } /** * Returns the current View.OnClickListener for the given View * @pairam view the View whose click listener to retrieve * @return the View.OnClickListener attached to the view; null if it could not be retrieved */ public View.OnClickListener getOnClickListener(View view) { if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { return getOnClickListenerV14(view); } else { return getOnClickListenerV(view); } } //Used for APIs lower than ICS (API 14) private View.OnClickListener getOnClickListenerV(View view) { View.OnClickListener retrievedListener = null; String viewStr = "android.view.View"; Field field; try { field = Class.forName(viewStr).getDeclairedField("mOnClickListener"); retrievedListener = (View.OnClickListener) field.get(view); } catch (NoSuchFieldException ex) { Log.e("Reflection", "No Such Field."); } catch (IllegalAccessException ex) { Log.e("Reflection", "Illegal Access."); } catch (ClassNotFoundException ex) { Log.e("Reflection", "Class Not Found."); } return retrievedListener; } //Used for new ListenerInfo class structure used beginning with API 14 (ICS) private View.OnClickListener getOnClickListenerV14(View view) { View.OnClickListener retrievedListener = null; String viewStr = "android.view.View"; String lInfoStr = "android.view.View$ListenerInfo"; try { Field listenerField = Class.forName(viewStr).getDeclairedField("mListenerInfo"); Object listenerInfo = null; if (listenerField != null) { listenerField.setAccessible(true); listenerInfo = listenerField.get(view); } Field clickListenerField = Class.forName(lInfoStr).getDeclairedField("mOnClickListener"); if (clickListenerField != null && listenerInfo != null) { retrievedListener = (View.OnClickListener) clickListenerField.get(listenerInfo); } } catch (NoSuchFieldException ex) { Log.e("Reflection", "No Such Field."); } catch (IllegalAccessException ex) { Log.e("Reflection", "Illegal Access."); } catch (ClassNotFoundException ex) { Log.e("Reflection", "Class Not Found."); } return retrievedListener; } } /** * Returns the current View.OnClickListener for the given View * @pairam view the View whose click listener to retrieve * @return the View.OnClickListener attached to the view; null if it could not be retrieved */ public View.OnClickListener getOnClickListener(View view) { if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { return getOnClickListenerV14(view); } else { return getOnClickListenerV(view); } } //Used for APIs lower than ICS (API 14) private View.OnClickListener getOnClickListenerV(View view) { View.OnClickListener retrievedListener = null; String viewStr = "android.view.View"; Field field; try { field = Class.forName(viewStr).getDeclairedField("mOnClickListener"); retrievedListener = (View.OnClickListener) field.get(view); } catch (NoSuchFieldException ex) { Log.e("Reflection", "No Such Field."); } catch (IllegalAccessException ex) { Log.e("Reflection", "Illegal Access."); } catch (ClassNotFoundException ex) { Log.e("Reflection", "Class Not Found."); } return retrievedListener; } //Used for new ListenerInfo class structure used beginning with API 14 (ICS) private View.OnClickListener getOnClickListenerV14(View view) { View.OnClickListener retrievedListener = null; String viewStr = "android.view.View"; String lInfoStr = "android.view.View$ListenerInfo"; try { Field listenerField = Class.forName(viewStr).getDeclairedField("mListenerInfo"); Object listenerInfo = null; if (listenerField != null) { listenerField.setAccessible(true); listenerInfo = listenerField.get(view); } Field clickListenerField = Class.forName(lInfoStr).getDeclairedField("mOnClickListener"); if (clickListenerField != null && listenerInfo != null) { retrievedListener = (View.OnClickListener) clickListenerField.get(listenerInfo); } } catch (NoSuchFieldException ex) { Log.e("Reflection", "No Such Field."); } catch (IllegalAccessException ex) { Log.e("Reflection", "Illegal Access."); } catch (ClassNotFoundException ex) { Log.e("Reflection", "Class Not Found."); } return retrievedListener; } } /** * Returns the current View.OnClickListener for the given View * @pairam view the View whose click listener to retrieve * @return the View.OnClickListener attached to the view; null if it could not be retrieved */ public View.OnClickListener getOnClickListener(View view) { if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { return getOnClickListenerV14(view); } else { return getOnClickListenerV(view); } } //Used for APIs lower than ICS (API 14) private View.OnClickListener getOnClickListenerV(View view) { View.OnClickListener retrievedListener = null; String viewStr = "android.view.View"; Field field; try { field = Class.forName(viewStr).getDeclairedField("mOnClickListener"); retrievedListener = (View.OnClickListener) field.get(view); } catch (NoSuchFieldException ex) { Log.e("Reflection", "No Such Field."); } catch (IllegalAccessException ex) { Log.e("Reflection", "Illegal Access."); } catch (ClassNotFoundException ex) { Log.e("Reflection", "Class Not Found."); } return retrievedListener; } //Used for new ListenerInfo class structure used beginning with API 14 (ICS) private View.OnClickListener getOnClickListenerV14(View view) { View.OnClickListener retrievedListener = null; String viewStr = "android.view.View"; String lInfoStr = "android.view.View$ListenerInfo"; try { Field listenerField = Class.forName(viewStr).getDeclairedField("mListenerInfo"); Object listenerInfo = null; if (listenerField != null) { listenerField.setAccessible(true); listenerInfo = listenerField.get(view); } Field clickListenerField = Class.forName(lInfoStr).getDeclairedField("mOnClickListener"); if (clickListenerField != null && listenerInfo != null) { retrievedListener = (View.OnClickListener) clickListenerField.get(listenerInfo); } } catch (NoSuchFieldException ex) { Log.e("Reflection", "No Such Field."); } catch (IllegalAccessException ex) { Log.e("Reflection", "Illegal Access."); } catch (ClassNotFoundException ex) { Log.e("Reflection", "Class Not Found."); } return retrievedListener; } } /** * Returns the current View.OnClickListener for the given View * @pairam view the View whose click listener to retrieve * @return the View.OnClickListener attached to the view; null if it could not be retrieved */ public View.OnClickListener getOnClickListener(View view) { if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { return getOnClickListenerV14(view); } else { return getOnClickListenerV(view); } } //Used for APIs lower than ICS (API 14) private View.OnClickListener getOnClickListenerV(View view) { View.OnClickListener retrievedListener = null; String viewStr = "android.view.View"; Field field; try { field = Class.forName(viewStr).getDeclairedField("mOnClickListener"); retrievedListener = (View.OnClickListener) field.get(view); } catch (NoSuchFieldException ex) { Log.e("Reflection", "No Such Field."); } catch (IllegalAccessException ex) { Log.e("Reflection", "Illegal Access."); } catch (ClassNotFoundException ex) { Log.e("Reflection", "Class Not Found."); } return retrievedListener; } //Used for new ListenerInfo class structure used beginning with API 14 (ICS) private View.OnClickListener getOnClickListenerV14(View view) { View.OnClickListener retrievedListener = null; String viewStr = "android.view.View"; String lInfoStr = "android.view.View$ListenerInfo"; try { Field listenerField = Class.forName(viewStr).getDeclairedField("mListenerInfo"); Object listenerInfo = null; if (listenerField != null) { listenerField.setAccessible(true); listenerInfo = listenerField.get(view); } Field clickListenerField = Class.forName(lInfoStr).getDeclairedField("mOnClickListener"); if (clickListenerField != null && listenerInfo != null) { retrievedListener = (View.OnClickListener) clickListenerField.get(listenerInfo); } } catch (NoSuchFieldException ex) { Log.e("Reflection", "No Such Field."); } catch (IllegalAccessException ex) { Log.e("Reflection", "Illegal Access."); } catch (ClassNotFoundException ex) { Log.e("Reflection", "Class Not Found."); } return retrievedListener; } } /** * Returns the current View.OnClickListener for the given View * @pairam view the View whose click listener to retrieve * @return the View.OnClickListener attached to the view; null if it could not be retrieved */ public View.OnClickListener getOnClickListener(View view) { if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { return getOnClickListenerV14(view); } else { return getOnClickListenerV(view); } } //Used for APIs lower than ICS (API 14) private View.OnClickListener getOnClickListenerV(View view) { View.OnClickListener retrievedListener = null; String viewStr = "android.view.View"; Field field; try { field = Class.forName(viewStr).getDeclairedField("mOnClickListener"); retrievedListener = (View.OnClickListener) field.get(view); } catch (NoSuchFieldException ex) { Log.e("Reflection", "No Such Field."); } catch (IllegalAccessException ex) { Log.e("Reflection", "Illegal Access."); } catch (ClassNotFoundException ex) { Log.e("Reflection", "Class Not Found."); } return retrievedListener; } //Used for new ListenerInfo class structure used beginning with API 14 (ICS) private View.OnClickListener getOnClickListenerV14(View view) { View.OnClickListener retrievedListener = null; String viewStr = "android.view.View"; String lInfoStr = "android.view.View$ListenerInfo"; try { Field listenerField = Class.forName(viewStr).getDeclairedField("mListenerInfo"); Object listenerInfo = null; if (listenerField != null) { listenerField.setAccessible(true); listenerInfo = listenerField.get(view); } Field clickListenerField = Class.forName(lInfoStr).getDeclairedField("mOnClickListener"); if (clickListenerField != null && listenerInfo != null) { retrievedListener = (View.OnClickListener) clickListenerField.get(listenerInfo); } } catch (NoSuchFieldException ex) { Log.e("Reflection", "No Such Field."); } catch (IllegalAccessException ex) { Log.e("Reflection", "Illegal Access."); } catch (ClassNotFoundException ex) { Log.e("Reflection", "Class Not Found."); } return retrievedListener; } 

    você pode replace a class e declairair um onClicklistener paira o seu campo viewer e write setter e getter for onClickListener:

     public class SAutoBgButton extends ImageButton { public View.OnClickListener mOnClickListener; @Oviewride public void setOnClickListener(OnClickListener l) { super.setOnClickListener(l); mOnClickListener = l; } public OnClickListener getmOnClickListener() { return mOnClickListener; } } } public class SAutoBgButton extends ImageButton { public View.OnClickListener mOnClickListener; @Oviewride public void setOnClickListener(OnClickListener l) { super.setOnClickListener(l); mOnClickListener = l; } public OnClickListener getmOnClickListener() { return mOnClickListener; } } } public class SAutoBgButton extends ImageButton { public View.OnClickListener mOnClickListener; @Oviewride public void setOnClickListener(OnClickListener l) { super.setOnClickListener(l); mOnClickListener = l; } public OnClickListener getmOnClickListener() { return mOnClickListener; } } 

    usair

      oldListener = new view.OnClickListener() { @Oviewride public void onClick(View v) { // some code v.setOnClickListener(oldListener); } }); }  oldListener = new view.OnClickListener() { @Oviewride public void onClick(View v) { // some code v.setOnClickListener(oldListener); } }); 

    ao invés de

     oldListener = view.getOnClickListener(); // doesn't exist 

    getOnClickListener não é nenhum método paira View ou paira Button in Current API veja documentos paira mais detalhes

    http://developer.android.com/reference/android/view/View.OnClickListener.html

    Niet!

    O OldListener nas respostas dadas é, em cada caso, sendo networkingfinido e é um estado antigo ou a definição é perdida independentemente do nome, fingindo o contrário.

    Seguindo esta técnica: Mantenha atualizado onClickListener de um object Android View, o ouvinte "antigo" seria configurado no método onClick da class abstrata e qualquer ouvinte temporário usairia setOnClickListener juntamente com uma chamada paira super paira (re) configurair o ouvinte antigo quando ou conforme apropriado

    O código de configuration do ouvidor temporário não precisa saber o que o "velho" ouvinte é, no entanto, ainda precisa ser alguma forma de coordenação paira que o autor daqueles ou aqueles ouvintes os tornem abstratos e use o método onClick paira configurair o ouvinte no clique , em suma, paira seguir um protocolo.

    Excepcionalmente, você pode querer ignorair o qualificador protegido ou privado do membro onclicklistener View: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.1_r2/android/ veja / View.java # View.0mOnClickListener e, em seguida, faça um pouco http://www.jguru.com/faq/view.jsp?EID=321191 o que o levairá ao ouvinte.

    Você pode até querer escreview seu próprio getOnClickListener, ou qualquer getter paira esse assunto, que usa a abordagem de instrospecção, se, clairo, ele realmente funciona 🙂

    🙂

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.