Android Butterknife 黄油刀的使用

Android Butterknife 黄油刀的使用

1、ButterKnife是一个由JakeWharton写的开源框架,它使用注解处理将属性和方法和View绑定,以生成模板代码。
Eliminate findViewById calls by using @BindView on fields.
Group multiple views in a list or array. Operate on all of them at once with actions, setters, or properties.
Eliminate anonymous inner-classes for listeners by annotating methods with @OnClick and others.
Eliminate resource lookups by using resource annotations on fields.

2、项目地址:https://github.com/JakeWharton/butterknife

3、基本配置
步骤1:在Project的 build.gradle 中添加butterknife:

buildscript {
    
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.4'
        classpath 'com.jakewharton:butterknife-gradle-plugin:9.0.0-rc1'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

步骤2:在App的 build.gradle 中添加butterknife:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.android.support:recyclerview-v7:27.1.0'
    implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.0.5.1'
    implementation 'com.scwang.smartrefresh:SmartRefreshHeader:1.0.5.1'
    implementation 'com.android.support:design:27.1.1'
    implementation 'com.squareup.okhttp3:okhttp:3.11.0'
    implementation 'com.squareup.okio:okio:1.15.0'
    implementation 'com.jakewharton:butterknife:9.0.0-rc1'
    annotationProcessor 'com.jakewharton:butterknife-compiler:9.0.0-rc1'
}

步骤3:在App的 build.gradle 中添加butterknife:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    implementation 'com.android.support:recyclerview-v7:28.0.0'
    implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.0.5.1'
    implementation 'com.scwang.smartrefresh:SmartRefreshHeader:1.0.5.1'
    implementation 'com.android.support:design:28.0.0'
    implementation 'com.squareup.okhttp3:okhttp:3.11.0'
    implementation 'com.squareup.okio:okio:1.15.0'
    implementation 'com.jakewharton:butterknife:9.0.0-rc1'
    annotationProcessor 'com.jakewharton:butterknife-compiler:9.0.0-rc1'
}

4、测试代码

@BindView(R.id.tv_login_msg)
TextView tvLoginMsg;

//private TextView tvLoginMsg;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    // 绑定activity
    ButterKnife.bind(this);

    //tvLoginMsg = (TextView) findViewById(R.id.tv_login_msg);
}

 

发表回复

您的电子邮箱地址不会被公开。