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>;
}
注意事项:
@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>;
}
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories