【问题标题】:Putting custom method to custom component将自定义方法放入自定义组件
【发布时间】:2013-12-09 11:33:44
【问题描述】:

我有自定义组件扩展了 LinearLayout。我需要创建属性来为这个组件放置自定义方法。我需要在组件内部调用它。

像按钮一样。

<Button android:onClick="customMethod"/>

如何解决?

【问题讨论】:

    标签: android custom-component


    【解决方案1】:

    你看过这篇文章吗:

    Android - Writing a custom (compound) component

    可能是您正在寻找的答案。

    【讨论】:

      【解决方案2】:

      查看Android java文件中View类的源码可以发现如下

                  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 方法应该是您正在寻找的。​​p>

      【讨论】:

        猜你喜欢
        • 2017-12-14
        • 1970-01-01
        • 1970-01-01
        • 2018-03-22
        • 2014-11-20
        • 1970-01-01
        • 2020-08-28
        • 2017-01-13
        • 2019-06-13
        相关资源
        最近更新 更多