您的位置:首页 > Web前端

butterknife 详解

2016-03-06 20:53 369 查看
教程1:butterknife 注解代码使用  ,注意在framement.java里面需要在创建时注册,销毁时撤销。

注意事项:

@Nullable @Bind(R.id.might_not_be_there) TextView mightNotBeThere;

使用@Nullable可规避在没用获取到view时异常错误。

在oncreate函数里面 ,加入

 setContentView(R.layout.simple_activity); 在这个后面。

 ButterKnife.bind(this);

然后代码使用为:

 @Bind(R.id.user) EditText username; 绑定一个元素

 @BindString(R.string.login_error)  绑定一个字串

  String loginErrorMessage;

 @Bind({ R.id.title, R.id.subtitle, R.id.hello })  绑定一个数组

  List<View> headerViews;

 @OnLongClick(R.id.hello) boolean sayGetOffMe() {   一个回调,回调可以使用callback类型指定具体绑定哪种类别。

    Toast.makeText(this, "Let go of me!", LENGTH_SHORT).show();

    return true;

  }

高级用法:给一组赋值

  private static final ButterKnife.Action<View> ALPHA_FADE = new ButterKnife.Action<View>() {

    @Override public void apply(@NonNull View view, int index) {

      AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);

      alphaAnimation.setFillBefore(true);

      alphaAnimation.setDuration(500);

      alphaAnimation.setStartOffset(index * 100);

      view.startAnimation(alphaAnimation);

    }

  };

ButterKnife.apply(headerViews, ALPHA_FADE);

  public interface Action<T extends View> {

    /** Apply the action on the {@code view} which is at {@code index} in the list. */

    void apply(T view, int index);

  }

  /** A setter that can apply a value to a list of views. */

  public interface Setter<T extends View, V> {

    /** Set the {@code value} on the {@code view} which is at {@code index} in the list. */

    void set(T view, V value, int index);

  }

使用方法:

GRADLE

compile 'com.jakewharton:butterknife:(insert latest version)'

Be sure to suppress this lint warning in your build.gradle.

lintOptions {

  disable 'InvalidPackage'

}

Some configurations may also require additional exclusions.

packagingOptions {

  exclude 'META-INF/services/javax.annotation.processing.Processor'

}

compile 'com.jakewharton:butterknife:7.0.1' 加入这个。

混淆使用:

-keep class butterknife.** { *; }

-dontwarn butterknife.internal.**

-keep class **$$ViewBinder { *; }

-keepclasseswithmembernames class * {

    @butterknife.* <fields>;

}

-keepclasseswithmembernames class * {

    @butterknife.* <methods>;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息