Android:与Butterknife的Kotlin

我正在尝试将Kotlin与Butterknife一起用于我的Android应用程序。

这是我的build.gradle

dependencies { ... compile 'com.jakewharton:butterknife:8.0.1' kapt 'com.jakewharton:butterknife-compiler:8.0.1' } kapt { generateStubs = true } 

我也有一个EditText,我想用ButterKnife在更改时显示一条消息:

 @OnTextChanged(R.id.input) fun test() { toast(1) } 

但是,没有任何反应。 我把一个断点放入函数中 – 甚至没有执行。

PS:我听说过kotterknife,但是我已经看到了纯粹的Butterknife的例子 。

我究竟做错了什么?

    7 Solutions collect form web for “Android:与Butterknife的Kotlin”

    Kotlin没有必要使用刀叉。 您可以直接使用以下内容:

    // app:build.gradle文件

     apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' apply plugin: 'kotlin-kapt' android { compileSdkVersion 26 buildToolsVersion "26.0.1" defaultConfig { applicationId "com.example.nikhiljadhav.myapplication" minSdkVersion 15 targetSdkVersion 26 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" implementation 'com.android.support:appcompat-v7:26.0.0' implementation 'com.android.support.constraint:constraint-layout:1.0.2' implementation 'com.android.support:design:26.0.0' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.0' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.0' } kapt { generateStubs = true } 

    // xml布局文件

     <TextView android:id="@+id/tvHello" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" android:layout_marginBottom="8dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toTopOf="parent" android:layout_marginTop="8dp" app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="8dp" app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="8dp" /> <TextView android:id="@+id/tvId" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" android:layout_marginBottom="8dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toTopOf="parent" android:layout_marginTop="8dp" app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="8dp" app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="8dp" /> <EditText android:id="@+id/etDemo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="8dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toTopOf="parent" android:layout_marginTop="8dp" app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="8dp" android:onClick="onClick" app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="8dp" /> 

    // MainActivity.kt文件

     class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) setSupportActionBar(toolbar) tvHello.setText("Hi bla bla") tvId.setText("buubububub") etDemo.setHint("nhdodfhfgf") tvId.setOnClickListener{ view-> onClick(view) } fab.setOnClickListener { view -> Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show() } } fun onClick(view: View) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show() } ... } 

    在你的应用程序级别build.gradle

     apply plugin: 'kotlin-android' kapt { generateStubs = true } dependencies { compile 'com.jakewharton:butterknife:8.4.0' kapt 'com.jakewharton:butterknife-compiler:8.4.0' } 

    在您的顶级build.gradle

     buildscript { ext.kotlin_version = '1.1.3' repositories { jcenter() } dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } 

    活动

     @BindView(R.id.toolbar) @JvmField var toolbar: Toolbar? = null 

    要么

     @BindView(R.id.toolbar) lateinit var toolbar: Toolbar 

    OnCreate里面

     ButterKnife.bind(this) 

    在你的gradle中:

     compile 'com.jakewharton:butterknife:8.8.0' kapt "com.jakewharton:butterknife-compiler:8.8.0" 

    在你的活动中

     @BindView(R.id.toolbar) lateinit var mToolbar: Toolbar 

    当然,请记住ButterKnife.bind(this)并在app.gradle apply plugin: 'kotlin-kapt'的顶部apply plugin: 'kotlin-kapt'

    检查完整的例子

    完整链接: https : //github.com/JetBrains/kotlin-examples/tree/master/gradle/android-butterknife

    Kotlin创作者在他们的网站上说:Kotlin Android Extensions插件(自动捆绑到Android Studio中的Kotlin插件)解决了同样的问题: 用简明直接的代码替代findViewById 考虑使用它,除非你已经使用ButterKnife并且不想迁移。

    和例如

     // Using R.layout.activity_main from the main source set import kotlinx.android.synthetic.main.activity_main.* class MyActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) textView.setText("Hello, world!") // Instead of findViewById(R.id.textView) as TextView } } 

    textViewActivity的扩展属性,它与activity_main.xml声明的类型相同。

    将其添加到您的Project Build.gradle中

     buildscript { ext.kotlin_version = '1.1.2-4' ext.butterknife_version = '8.6.0' repositories { maven { url 'https://maven.google.com' } jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.0.0-alpha1' 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 } } 

    并在您的应用程序Build.Gradle添加此。

      //Butterknife compile "com.jakewharton:butterknife:$butterknife_version" kapt "com.jakewharton:butterknife-compiler:$butterknife_version" 

    你可以实现一些扩展来改善你的视图行为。 在常规的editText中检查“onTextChange”的例子:

     fun EditText.onTextChange(callback: (text: CharSequence?, start: Int, before: Int, count: Int) -> Unit) { addTextChangedListener(object : TextWatcher { override fun afterTextChanged(s: Editable?) {} override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {} override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { callback(s, start, before, count) } }) } 

    用法:

     m_editText.onTextChange { text, _, _, _ -> m_textView.text = text } 

    我投了kotlin-android-extensions

    Kotlin language will be the best programming language for Android.