diff options
| author | Romain Guy <romainguy@google.com> | 2009-05-01 10:57:14 -0700 |
|---|---|---|
| committer | Romain Guy <romainguy@google.com> | 2009-05-01 10:57:14 -0700 |
| commit | 9a81736176f507489f262344b6e3d55bbe53be7c (patch) | |
| tree | 1b12569fc3d58eabac132452f06011dd46d3d530 /core/java/android/view | |
| parent | 15d98b23ca83e7cd25ee14945e55c2701e75994f (diff) | |
| download | frameworks_base-9a81736176f507489f262344b6e3d55bbe53be7c.zip frameworks_base-9a81736176f507489f262344b6e3d55bbe53be7c.tar.gz frameworks_base-9a81736176f507489f262344b6e3d55bbe53be7c.tar.bz2 | |
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.
Diffstat (limited to 'core/java/android/view')
| -rw-r--r-- | core/java/android/view/View.java | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/core/java/android/view/View.java b/core/java/android/view/View.java index 894045a..07c56ee 100644 --- a/core/java/android/view/View.java +++ b/core/java/android/view/View.java @@ -62,6 +62,8 @@ import com.android.internal.view.menu.MenuBuilder; import java.util.ArrayList; import java.util.Arrays; import java.lang.ref.SoftReference; +import java.lang.reflect.Method; +import java.lang.reflect.InvocationTargetException; /** * <p> @@ -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; } } |
