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.
This commit is contained in:
@ -5344,6 +5344,17 @@
|
||||
visibility="public"
|
||||
>
|
||||
</field>
|
||||
<field name="onClick"
|
||||
type="int"
|
||||
transient="false"
|
||||
volatile="false"
|
||||
value="16843375"
|
||||
static="true"
|
||||
final="true"
|
||||
deprecated="not deprecated"
|
||||
visibility="public"
|
||||
>
|
||||
</field>
|
||||
<field name="oneshot"
|
||||
type="int"
|
||||
transient="false"
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1165,6 +1165,13 @@
|
||||
enabled for events such as long presses. -->
|
||||
<attr name="hapticFeedbackEnabled" format="boolean" />
|
||||
|
||||
<!-- Name of the method in this View's context to invoke when the view is
|
||||
clicked. This name must correspond to a public method that takes
|
||||
exactly one parameter of type View. For instance, if you specify
|
||||
<code>android:onClick="sayHello"</code>, you must declare a
|
||||
<code>public void sayHello(View v)</code> method of your context
|
||||
(typically, your Activity.)-->
|
||||
<attr name="onClick" format="string" />
|
||||
</declare-styleable>
|
||||
|
||||
<!-- Attributes that can be used with a {@link android.view.ViewGroup} or any
|
||||
|
@ -1096,6 +1096,7 @@
|
||||
<public type="attr" name="density" id="0x0101026c" />
|
||||
<public type="attr" name="searchSuggestThreshold" id="0x0101026d" />
|
||||
<public type="attr" name="includeInGlobalSearch" id="0x0101026e" />
|
||||
<public type="attr" name="onClick" id="0x0101026f" />
|
||||
|
||||
<public type="anim" name="anticipate_interpolator" id="0x010a0007" />
|
||||
<public type="anim" name="overshoot_interpolator" id="0x010a0008" />
|
||||
|
Reference in New Issue
Block a user