最新消息:关注【已取消】微信公众号,可以获取全套资料,【全套Java基础27天】【JavaEE就业视频4个月】【Android就业视频4个月】

butterknife框架的使用方法介绍

Android 太平洋学习网 浏览 评论

Android butterknife(Butter Knife)框架是一个注解式编程框架,可以很好的使用@BindView代替findViewById,使用@OnClick代替setOnClickListener等事件,既节约了写代码的时间,也减少了过多的代码量,这也是Java注解式编程的好处,下面来学习一下butterknife框架吧!

1:首先在build.gradle里面导入Butter Knife的jar包,添加如下代码:

implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'

如果导入的butterknife版本过新,如butterknife:10.2.3版本,就会报如下错误,所以小编只使用了butterknife:8.8.1作为演示:

Duplicate class android.support.v4.app.INotificationSideChannel found in modules classes.jar (androidx.core:core:1.0.0) and classes.jar (com.android.support:support-compat:27.1.1)

Duplicate class android.support.v4.app.INotificationSideChannel$Stub found in modules classes.jar (androidx.core:core:1.0.0) and classes.jar (com.android.support:support-compat:27.1.1)

Duplicate class android.support.v4.app.INotificationSideChannel$Stub$Proxy found in modules classes.jar (androidx.core:core:1.0.0) and classes.jar (com.android.support:support-compat:27.1.1)

Duplicate class android.support.v4.os.IResultReceiver found in modules classes.jar (androidx.core:core:1.0.0) and classes.jar (com.android.support:support-compat:27.1.1)

Duplicate class android.support.v4.os.IResultReceiver$Stub found in modules classes.jar (androidx.core:core:1.0.0) and classes.jar (com.android.support:support-compat:27.1.1)

Duplicate class android.support.v4.os.IResultReceiver$Stub$Proxy found in modules classes.jar (androidx.core:core:1.0.0) and classes.jar (com.android.support:support-compat:27.1.1)

Duplicate class android.support.v4.os.ResultReceiver found in modules classes.jar (androidx.core:core:1.0.0) and classes.jar (com.android.support:support-compat:27.1.1)

Duplicate class android.support.v4.os.ResultReceiver$1 found in modules classes.jar (androidx.core:core:1.0.0) and classes.jar (com.android.support:support-compat:27.1.1)

Duplicate class android.support.v4.os.ResultReceiver$MyResultReceiver found in modules classes.jar (androidx.core:core:1.0.0) and classes.jar (com.android.support:support-compat:27.1.1)

Duplicate class android.support.v4.os.ResultReceiver$MyRunnable found in modules classes.jar (androidx.core:core:1.0.0) and classes.jar (com.android.support:support-compat:27.1.1)


Go to the documentation to learn how to Fix dependency resolution errors.

2:可以使用@BindView注解绑定一些属性,使用@OnClick绑定点击事件,如下:

绑定属性:

class ExampleActivity extends Activity {
  @BindView(R2.id.user) EditText username;
  @BindView(R2.id.pass) EditText password;
...
}

绑定点击事件:

  @OnClick(R.id.submit) void submit() {   
   // TODO call server...
  }

当然了,你还可以同时绑定N多个点击事件,代码如下:

    @OnClick({R.id.btn_mesage, R.id.btn_image, R.id.btn_list, R.id.btn_dialog1, R.id.btn_dialog2})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_mesage:

                break;
            case R.id.btn_image:

                break;
            case R.id.btn_list:

                break;
            case R.id.btn_dialog1:

                break;
            case R.id.btn_dialog2:

                break;
            default:
                break;
      }

3:butterknife还有一些别的注解,如:@BindColor, @BindString, @BindAnim等,具体的用法可以自己Google一下,如图所示:

image.png

来源网站:太平洋学习网,转载请注明出处:http://www.tpyyes.com/a/android/1045.html
"文章很值,打赏犒劳作者一下"
微信号: Javaweb_engineer

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

与本文相关的文章

发表我的评论
取消评论

表情

您的回复是我们的动力!

  • 昵称 (必填)

网友最新评论