1
我有自定義組件擴展了LinearLayout。我需要爲此組件添加自定義方法的創建屬性。我需要在組件內部調用它。將自定義方法放入自定義組件
像按鈕一樣。
<Button android:onClick="customMethod"/>
如何解決?
我有自定義組件擴展了LinearLayout。我需要爲此組件添加自定義方法的創建屬性。我需要在組件內部調用它。將自定義方法放入自定義組件
像按鈕一樣。
<Button android:onClick="customMethod"/>
如何解決?
縱觀View
類的在Android源代碼的Java文件,你可以找到以下
case R.styleable.View_onClick:
if (context.isRestricted()) {
throw new IllegalStateException("The android:onClick attribute cannot "
+ "be used within a restricted context");
}
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) {
int id = getId();
String idText = id == NO_ID ? "" : " with id '"
+ getContext().getResources().getResourceEntryName(
id) + "'";
throw new IllegalStateException("Could not find a method " +
handlerName + "(View) in the activity "
+ getContext().getClass() + " for onClick handler"
+ " on view " + View.this.getClass() + idText, 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;
的getMethod
方法應該是你在找什麼。