(AS笔记)AndroidStudio中ButterKnife配置和使用

目录

  • 1.前言
  • 2.配置ButterKnife
  • 3.Activity绑定ButterKnife
  • 4.Fragment绑定ButterKnife
  • 5.总结


1.前言

        ButterKnife是一个专注于Android系统的View注入框架,以前总是要写很多findViewById来找到View对象,有了ButterKnife可以很轻松的省去这些步骤。


2.配置ButterKnife

        (1) 工程根目录外层的build.gradle:

添加:classpath 'com.jakewharton:butterknife-gradle-plugin:10.2.3'

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:7.0.0"
        //butterknife
        classpath 'com.jakewharton:butterknife-gradle-plugin:10.2.3'
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

        (2)工程根目录app内层的build.gradle:

AndroidStudio版本不同,添加方式不同

        方式一:id 'com.jakewharton.butterknife'

        方式二:apply plugin: 'com.jakewharton.butterknife'

        方式一:最顶端

//Android应用程序
apply plugin: 'com.android.application'
//butterknife
apply plugin: 'com.jakewharton.butterknife'
android {
    compileSdk 31
    ...
    ...
}        

        方式二:最顶端

plugins {
    //Android应用程序
    id 'com.android.application'
    //butterknife
    id 'com.jakewharton.butterknife'
}
android {
    compileSdk 31
    ...
    ...
}

      (3)dependencies

implementation 'com.jakewharton:butterknife:10.2.3'

annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.3'

dependencies {
    ...
    ...
    ...
    //butterknife
    implementation 'com.jakewharton:butterknife:10.2.3'
    annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.3'
}

3.Activity绑定ButterKnife

        说明:绑定使用ButterKnife.bind(this);必须在setContentView();之后绑定;且父类bind绑定后,子类不需要再bind

        (1)创建Activity基类BaseActivity

/**
 * created by on 2021/8/11
 * 描述:Activity基类
 *
 * @author ZSAndroid
 * @create 2021-08-11-16:37
 */
public class BaseActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //绑定ButterKnife.必须在setContentView之后
        ButterKnife.bind(this);
    }
}

        (2)LoginActivity继承BaseActivity时,不用再次ButterKnife.bind(this);绑定LoginActivity

/**
 * created by on 2021/8/11
 * 描述:
 *
 * @author ZSAndroid
 * @create 2021-08-11-4:27
 */
public class LoginActivity extends BaseActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
    }
}

3.Fragment绑定ButterKnife 

        说明:Fragment中必须在onDestroyView()中使用unbinder.unbind();做解绑操作。ButterKnife.bind(this,view);这里的this不能替换getActivity()

/**
 * created by on 2021/8/11
 * 描述:Fragment基类
 *
 * @author ZSAndroid
 * @create 2021-08-11-16:53
 */
public class BaseFragment extends Fragment {

    private Unbinder unbinder;
    
    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
        //返回一个Unbinder值(进行解绑),注意这里的this不能使用getActivity()
        View view = inflater.inflate(R.layout.fragment_test, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    /**
     * onDestroyView中进行解绑操作
     */
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}

5.总结

仅自己学习记录,如有错误,敬请谅解~,谢谢~~~

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
Android StudioButterKnife是一个用于简化Android开发视图绑定的开源库。它可以帮助开发者通过注解方式快速地绑定视图资源,减少findViewById使用,提高开发效率。 使用ButterKnife,首先需要在项目的build.gradle文件添加依赖: ```groovy dependencies { implementation 'com.jakewharton:butterknife:10.2.1' annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.1' } ``` 然后,在需要使用ButterKnifeActivityFragment使用`@BindView`注解来绑定视图资源。 例如,在Activity绑定一个TextView: ```java public class MainActivity extends AppCompatActivity { @BindView(R.id.textView) TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); // 视图绑定 // 使用textView textView.setText("Hello ButterKnife!"); } } ``` 在Fragment使用类似的方式绑定视图资源。 需要注意的是,使用ButterKnife进行视图绑定时,必须在`setContentView()`之后调用`ButterKnife.bind(this)`来完成绑定。 除了`@BindView`注解外,ButterKnife还提供了其他注解,如`@OnClick`用于点击事件的绑定、`@Nullable`用于可为空的注解等。 值得一提的是,自从Android Studio 3.6版本起,Google推出了ViewBinding功能,它提供了类似ButterKnife的视图绑定功能,并且是官方支持的。如果使用最新版本的Android Studio,建议使用ViewBinding来代替ButterKnife
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

电竞丶小松哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值