Andrews butter knife

This article will explain how to how to use ButerKnife in kotlin in, presumably in the java code to use ButterKnife we ​​all play fast, then how to configure it in kotlin in how to use KuterKnife, it might there are some small partners is not very familiar with, so here I will introduce here, if how to use Kotlin in.

 

text:

         Earlier he published the article has been introduced, if ButterKnife to use the java code

        Android Studio binding ButterKnife Zelezny efficient use Butterknife (butter knife) 

        If the configuration is also introduced in Android Studio in Kontlin 

        Android Studio configuration uses Kotlin

 

If you are not familiar, you can watch it more than two articles.

 

Well, then directly introduced at the beginning of Kotlin in ButterKnife to use it.

It can be configured using configuration environment good Kotlin Kotlin language According to the article Android Studio.

 

The first step: in the project to configure the code in gradle
 

buildscript {
ext.kotlin_version = '1.3.11'
ext.butterknife_version='8.6.0'

repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "com.jakewharton:butterknife-gradle-plugin:$butterknife_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}


 

Step Two: In gradle under app module configured ButterKnife added and application widget reference Kotlin

(It should be noted here is incorporated herein by reference into the front of ButterKnife-comiler not apt now kapt)

apply plugin: 'com.android.application'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android'



implementation 'com.jakewharton:butterknife:8.8.1'
kapt "com.jakewharton:butterknife-compiler:8.8.1"


 

Step 3: Start using (layout files just write a few controls, mainly show you)

 

 

 

 

Then I'll just paste the code

 

MainActivity class: AppCompatActivity () {

// use ButterKnife lookup control
@BindView (R.id.text_view) lateinit var textView: TextView

@BindView (R.id.one) lateinit var One: the Button;

var unbinder: Unbinder = null;?

Fun the onCreate the override (savedInstanceState:? Bundle) {
super.onCreate (savedInstanceState)
setContentView (R.layout.activity_main)
// registered ButterKnife
unbinder = ButterKnife.bind (the this)
}


/ **
* Kotlin support higher-order functions and Lambda expression type
* If you are not familiar with here, then, can be Baidu, here is not to do too much to explain the
* /
@OnClick (R.id.one, R.id.two, R.id.three)
Fun onClick (View : View) {
When (view.id) {
R.id.one -> {
Toast.makeText (the this, "One", Toast.LENGTH_SHORT) the .Show ()
}
R.id.two ->{
Toast.makeText(this,"two",Toast.LENGTH_SHORT).show()
}
R.id.three ->{
Toast.makeText(this,"three",Toast.LENGTH_SHORT).show()
}
}
}

override fun onDestroy() {
super.onDestroy()
unbinder!!.unbind()
}

}
 

Well, then run it to see the effect of it

 

 

 

to sum up:

         Well, here it introduced over, but something does not feel small, it is fast plugin. We can only quick plug-in to quickly generate java code, find the controls, as well as a way to add a click event

That is ButterKnife Zelezny (use Check out the Android Studio combined ButterKnife Zelezny efficient use Butterknife (butter knife)).

        Then there is no comparable in Kotlin plug it in, we can reduce the Hand code time, amount ...... ..... pity here, I have not found a similar plug-in tools. If you know you are welcome to comment remind oh ~
---------------------
Author: WL- ghost
Source: CSDN
Original: https: //blog.csdn.net / qq_35070105 / article / details / 78821185
copyright: This article is a blogger original article, reproduced, please attach Bowen link!

Guess you like

Origin www.cnblogs.com/vana/p/11228283.html