ButterKnife源码解析之---基础文档

ButterKnife的基本使用请参考官方文档,不做过多介绍。其实第三方库在使用的时候,我们都可以从他的源码文档里,找到使用示例,所以我们先从源码的头文档分析起。我们看看源码文档里都给我们介绍了如何使用呢?ButterKnife类头文档原文如下:

/**
 * Field and method binding for Android views. Use this class to simplify finding views and
 * attaching listeners by binding them with annotations.
 * <p>
 * Finding views from your activity is as easy as:
 * <pre><code>
 * public class ExampleActivity extends Activity {
 *   {@literal @}BindView(R.id.title) EditText titleView;
 *   {@literal @}BindView(R.id.subtitle) EditText subtitleView;
 *
 *   {@literal @}Override protected void onCreate(Bundle savedInstanceState) {
 *     super.onCreate(savedInstanceState);
 *     setContentView(R.layout.example_activity);
 *     ButterKnife.bind(this);
 *   }
 * }
 * </code></pre>
 * Binding can be performed directly on an {@linkplain #bind(Activity) activity}, a
 * {@linkplain #bind(View) view}, or a {@linkplain #bind(Dialog) dialog}. Alternate objects to
 * bind can be specified along with an {@linkplain #bind(Object, Activity) activity},
 * {@linkplain #bind(Object, View) view}, or
 * {@linkplain #bind(Object, android.app.Dialog) dialog}.
 * <p>
 * Group multiple views together into a {@link List} or array.
 * <pre><code>
 * {@literal @}BindView({R.id.first_name, R.id.middle_name, R.id.last_name})
 * List<EditText> nameViews;
 * </code></pre>
 * <p>
 * To bind listeners to your views you can annotate your methods:
 * <pre><code>
 * {@literal @}OnClick(R.id.submit) void onSubmit() {
 *   // React to button click.
 * }
 * </code></pre>
 * Any number of parameters from the listener may be used on the method.
 * <pre><code>
 * {@literal @}OnItemClick(R.id.tweet_list) void onTweetClicked(int position) {
 *   // React to tweet click.
 * }
 * </code></pre>
 * <p>
 * Be default, views are required to be present in the layout for both field and method bindings.
 * If a view is optional add a {@code @Nullable} annotation for fields (such as the one in the
 * <a href="http://tools.android.com/tech-docs/support-annotations">support-annotations</a> library)
 * or the {@code @Optional} annotation for methods.
 * <pre><code>
 * {@literal @}Nullable @BindView(R.id.title) TextView subtitleView;
 * </code></pre>
 * Resources can also be bound to fields to simplify programmatically working with views:
 * <pre><code>
 * {@literal @}BindBool(R.bool.is_tablet) boolean isTablet;
 * {@literal @}BindInt(R.integer.columns) int columns;
 * {@literal @}BindColor(R.color.error_red) int errorRed;
 * </code></pre>
 */

通过文档我们可以知道:

1.在activity中使用如下:

     public class ExampleActivity extends Activity {
        //给需要初始化的view添加注解,参数为id
        @BindView(R.id.title)
        EditText titleView;
        @BindView(R.id.subtitle)
        EditText subtitleView;

        Override
        protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.example_activity);
          //绑定,而且要在setcontentview后面
          ButterKnife.bind(this);
       }
     }

2.我们还可以绑定其他的,例如:

            ButterKnife.bind(view);
            ButterKnife.bind(dialog);
            ButterKnife.bind(objcet);
            ButterKnife.bind(Object, Activity);
            ButterKnife.bind(Object, android.app.Dialog);

当然这些方法都是重载方法,可以去看文档介绍。

3.我们还可以将多个元素绑定到一个集合中:

@BindView({R.id.first_name, R.id.middle_name, R.id.last_name})
List<EditText> nameViews;

4.还可以绑定点击事件、列表的item点击事件:

        @OnClick(R.id.submit) 
        void onSubmit() {
        // React to button click.
        }

        @OnItemClick(R.id.tweet_list)
        void onTweetClicked(int position) {
            // React to tweet click.
        }

5.还可以绑定可选控件,就是说如果这个控件找不到也不会报错

@Nullable @BindView(R.id.title)
TextView subtitleView;


6.还可以绑定其他资源类

        @BindBool(R.bool.is_tablet) boolean isTablet;
        @BindInt(R.integer.columns) int columns;
        @BindColor(R.color.error_red) int errorRed;

好了,以上就是官方文档给我们介绍的如何使用ButterKnife。那么到底ButterKnife都可以用来绑定什么呢?

我们可以看到一共可以绑定这么多,并且每个类中都给出了如何使用的文档介绍。这里就随便找一个OnCheckedChanged 看看:

/**
 * Bind a method to an {@link OnCheckedChangeListener OnCheckedChangeListener} on the view for
 * each ID specified.
 * <pre><code>
 * {@literal @}OnCheckedChanged(R.id.example) void onChecked(boolean checked) {
 *   Toast.makeText(this, checked ? "Checked!" : "Unchecked!", Toast.LENGTH_SHORT).show();
 * }
 * </code></pre>
 * Any number of parameters from
 * {@link OnCheckedChangeListener#onCheckedChanged(android.widget.CompoundButton, boolean)
 * onCheckedChanged} may be used on the method.
 *
 * @see OnCheckedChangeListener
 */
@Target(METHOD)
@Retention(RUNTIME)
@ListenerClass(
    targetType = "android.widget.CompoundButton",
    setter = "setOnCheckedChangeListener",
    type = "android.widget.CompoundButton.OnCheckedChangeListener",
    method = @ListenerMethod(
        name = "onCheckedChanged",
        parameters = {
            "android.widget.CompoundButton",
            "boolean"
        }
    )
)
public @interface OnCheckedChanged {
  /** View IDs to which the method will be bound. */
  @IdRes int[] value() default { View.NO_ID };
}

 OnCheckedChanged这个我们可以看到文档上面写有,作用于CompounButton的setOnCheckedChangedListener.使用方法如下:

@OnCheckedChanged(R.id.example)
    void onChecked(boolean checked) {
        Toast.makeText(this, checked ? "Checked!" : "Unchecked!",Toast.LENGTH_SHORT).show();
}

好了,文档介绍完了。其实上面介绍文档挺枯燥的,没什么意思。不过我之所以写出来,是想说一下我解析源码的思路,首先就是先看看官方文档里介绍如何使用的,然后看看都可以怎么用。先知道怎么用,再分析原理。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值