AndroidKotlin环境如何使用ButterKnife

这篇文章将为大家详细讲解有关Android Kotlin环境如何使用ButterKnife,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

松岭网站建设公司成都创新互联公司,松岭网站设计制作,有大型网站制作公司丰富经验。已为松岭上千多家提供企业网站建设服务。企业网站搭建\外贸营销网站建设要多少钱,请找那个售后服务好的松岭做网站的公司定做!

先看看java环境的用法

1.安装插件,然后重启Android studio。

Android Kotlin环境如何使用ButterKnife

安装插件.jpg

2.使用,点击一下在setContentView(R.layout.activity_main);然后快捷键Alt+insert。

Android Kotlin环境如何使用ButterKnife

Alt+insert.jpg

3.使用。

Android Kotlin环境如何使用ButterKnife

使用.jpg

4.完成

@BindView(R.id.circleView)
CircleView circleView;
@BindView(R.id.tv_test)
TextView tvTest;
 
@Override
protected void onCreate(Bundle savedInstanceState)
{
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  ButterKnife.bind(this);
}
 
/*
* 多个控件点击事件,会有大括号包起来,中间逗号隔开
* */
@OnClick({R.id.circleView, R.id.tv_test})
public void onViewClicked(View view)
{
  switch (view.getId())
  {
    case R.id.circleView:
      break;
    case R.id.tv_test:
      break;
  }
}
 
@Override
protected void onDestroy()
{
  ButterKnife.bind(this).unbind();
  super.onDestroy();
}

Kotlin环境

1.随意新建一个kotlin文件

Android Kotlin环境如何使用ButterKnife

new kotlin.jpg

2.然后工程会有几个地方变化

Android Kotlin环境如何使用ButterKnife

多了Configure.jpg

Android Kotlin环境如何使用ButterKnife

ok.jpg

Android Kotlin环境如何使用ButterKnife

module.jpg

Android Kotlin环境如何使用ButterKnife

project.jpg

3.添加对kotlin的支持

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
 
dependencies {
  implementation fileTree(dir: 'libs', include: ['*.jar'])
  implementation 'com.android.support:appcompat-v7:26.1.0'
  implementation 'com.android.support.constraint:constraint-layout:1.0.2'
  //控件插件
  implementation 'com.jakewharton:butterknife:8.8.1'
  kapt 'com.jakewharton:butterknife-compiler:8.8.1'
  compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
}

Android Kotlin环境如何使用ButterKnife

添加kotlin支持.jpg

4.使用,直接用就好,不用再BindView了。

Android Kotlin环境如何使用ButterKnife

不需要BindView了.jpg

Android Kotlin环境如何使用ButterKnife

直接使用.jpg

5.注意要对应的xml,以免同样的控件id引起空指针异常。

Android Kotlin环境如何使用ButterKnife

关于“Android Kotlin环境如何使用ButterKnife”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。


网页名称:AndroidKotlin环境如何使用ButterKnife
网址分享:http://lszwz.com/article/giesge.html

其他资讯

售后响应及时

7×24小时客服热线

数据备份

更安全、更高效、更稳定

价格公道精准

项目经理精准报价不弄虚作假

合作无风险

重合同讲信誉,无效全额退款