Android Kotlin环境如何使用ButterKnife

发布时间:2021-08-07 11:35:02 作者:小新
来源:亿速云 阅读:172

这篇文章将为大家详细讲解有关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”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

推荐阅读:
  1. Android Studio常用插件有哪些
  2. Android Gradle依赖管理、去除重复依赖、忽略的方式有哪些

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

kotlin butterknife android

上一篇:shell中怎么实现SSH自动登陆

下一篇:如何解决某些HTML字符打不出来的问题

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》