android 添加黄油刀,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);

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
黄油ButterKnife)是一款Android视图的字段和方法绑定快速注解框架。它是由JakeWharton开发的,可以帮助Android开发者简化代码,省去繁琐的findViewById操作。 使用黄油可以在代码中使用注解的方式来绑定视图,避免了手动查找和绑定视图的过程,提高了开发效率。在使用黄油前,需要在build.gradle文件中添加依赖: implementation 'com.jakewharton:butterknife:10.2.3'// 添加此依赖 annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.3'// 添加此规则 然后,在需要使用黄油的Activity或Fragment中,可以通过注解的方式绑定视图。黄油使用的注解并不是在运行时反射的,而是在编译时生成新的class,所以对性能基本没有损失。 总的来说,黄油是一个方便易用的Android视图绑定框架,可以帮助开发者简化代码,提高开发效率。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Android Butterknife黄油使用方法总结](https://blog.csdn.net/donkor_/article/details/77879630)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [AndroidButterKnife黄油使用详解](https://blog.csdn.net/ojbk99267710/article/details/126098708)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值