ButterKnife8.0.2的使用例程

本文详细介绍了如何在 Android 开发中使用 ButterKnife 8.0.1 版本,包括配置 build.gradle 文件引入依赖及注解处理器,并展示了如何在 Activity 中绑定视图元素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在使用JakeWharton大神的BufferKnife(github地址:https://github.com/JakeWharton/butterknife),发现这个库太好用了,彻底摆脱了findViewById()这个这么长的方法。但是在版本8.0.0之前都是直接Compile “xxxxxxxxxxx”就可是使用了,但是8.0.0之后的依赖引用有点区别了。

PS:发现自己的工程取名字的时候把Butter写成了Buffer了!凑合看

首先在工程级build.gradle文件中添加:

buildscript {
  repositories {
    mavenCentral()
   }
  dependencies {
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
  }
}
下面是添加后的文件内容:


然后在Module级的build.gradle中添加:

apply plugin: 'android-apt'    //这个必须在文件顶部添加

compile 'com.jakewharton:butterknife:8.0.1'
apt 'com.jakewharton:butterknife-compiler:8.0.1'

添加后文件部分内容


现在就可以开始使用了

在布局文件中添加一个textview

<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />
在activity中

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.text)
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        textView.setText("1111111111");
    }

}
程序结果


源代码


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值