From 9a81736176f507489f262344b6e3d55bbe53be7c Mon Sep 17 00:00:00 2001 From: Romain Guy Date: Fri, 1 May 2009 10:57:14 -0700 Subject: [PATCH] Add the ability to specify the onClick handler with XML. The new android:onClick attribute defines the name of the method in the Activity to invoke when the button is clicked. The method has to be public and get one View parameter. --- api/current.xml | 11 +++++++++++ core/java/android/view/View.java | 32 ++++++++++++++++++++++++++++++++ core/res/res/values/attrs.xml | 7 +++++++ core/res/res/values/public.xml | 1 + 4 files changed, 51 insertions(+) diff --git a/api/current.xml b/api/current.xml index 93b3273c6b257..5080ca3e59061 100644 --- a/api/current.xml +++ b/api/current.xml @@ -5344,6 +5344,17 @@ visibility="public" > + + @@ -1874,6 +1876,36 @@ public class View implements Drawable.Callback, KeyEvent.Callback { case R.styleable.View_minHeight: mMinHeight = a.getDimensionPixelSize(attr, 0); break; + case R.styleable.View_onClick: + final String handlerName = a.getString(attr); + if (handlerName != null) { + setOnClickListener(new OnClickListener() { + private Method mHandler; + + public void onClick(View v) { + if (mHandler == null) { + try { + mHandler = getContext().getClass().getMethod(handlerName, + View.class); + } catch (NoSuchMethodException e) { + throw new IllegalStateException("Could not find a method " + + handlerName + "(View) in the activity", e); + } + } + + try { + mHandler.invoke(getContext(), View.this); + } catch (IllegalAccessException e) { + throw new IllegalStateException("Could not execute non " + + "public method of the activity", e); + } catch (InvocationTargetException e) { + throw new IllegalStateException("Could not execute " + + "method of the activity", e); + } + } + }); + } + break; } } diff --git a/core/res/res/values/attrs.xml b/core/res/res/values/attrs.xml index 3d7f4066b393e..633a831f0b184 100644 --- a/core/res/res/values/attrs.xml +++ b/core/res/res/values/attrs.xml @@ -1165,6 +1165,13 @@ enabled for events such as long presses. --> + +