AndroidStudio3.5 ButterKnife10.2.0配置与使用

前言

介绍:Android开发 View注入 IOC框架

优点:

  1. 使用ButterKnife对性能基本没有损失,因为ButterKnife用到的注解并不是在运行时反射的,而是在编译的时候生成新的class。

  2. 简化View绑定、Click事件处理功能、Adapter的ViewHolder绑定。

  3. 代码可读性强

Github:https://github.com/JakeWharton/butterknife

Doc:http://jakewharton.github.io/butterknife/

MyDocRepo(私人仓库备份):https://yankeyon.gitee.io/Doc/Butter%20Knife.htm

配置

AndroidStudio版本:v3.5

appSdk版本配置信息:

compileSdkVersion 28
defaultConfig {
    applicationId "com.keyon.myhandler"
    minSdkVersion 24
    targetSdkVersion 28
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
  1. 安装AS ButterKnife插件,提高开发效率。(安装后需重启IDE)

  2. 项目引入jar包

    android {
      ...
      // Butterknife requires Java 8.
      compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
      }
    }
    dependencies {
    	implementation 'com.jakewharton:butterknife:10.2.0'
        annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.0'
    }

     

  3. add the plugin to your buildscript

    buildscript {
      repositories {
        mavenCentral()
        google()
       }
      dependencies {
        classpath 'com.jakewharton:butterknife-gradle-plugin:10.2.0'
      }
    }

     

  4. apply it in your module

    apply plugin: 'com.jakewharton.butterknife'

     

使用

 

  1. 右键布局文件 -> Generate...(Alt+Insert)

  2. OnClick为是否生成点击事件函数,点击Confirm确认生成。

  3. 生成代码

    @BindView(R.id.tv_item_content)
    TextView tvItemContent;
    @BindView(R.id.btn_show_menu)
    Button btnShowMenu;
    
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
    }
    
    @OnClick(R.id.btn_show_menu)
    public void onViewClicked() {
        
    }

     

其他

除了View绑定注解与事件点击注解外,还有其他注解。

@BindArray 绑定string里面array数组;@BindArray(R.array.city ) String[] citys ;

@BindBitmap 绑定图片资源为Bitmap;@BindBitmap( R.mipmap.wifi ) Bitmap bitmap;

@BindString 绑定一个String id为一个String变量;@BindString( R.string.app_name ) String meg;

@BindColor 绑定color;@BindColor(R.color.colorAccent) int black;

@BindDimen 绑定Dimen;@BindDimen(R.dimen.borth_width) int mBorderWidth;

@BindDrawable 绑定Drawable;@BindDrawable(R.drawable.test_pic) Drawable mTestPic;

@BindFloat 绑定float

@BindInt 绑定int

@BindBool 绑定boolean值

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值