本文介绍了Android:Kotlin和Butterknife的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to use Kotlin with Butterknife for my Android Application.

这是我的build.gradle

Here is my build.gradle

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

kapt {
    generateStubs = true
}

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

I also has an EditText and I want to show a message using ButterKnife when it is changed:

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

但是,什么也没发生.我在函数中添加了一个断点-甚至没有执行.

However, nothing happens. I put a breakpoint into the function - and it is not even executed.

PS:我听说过kotterknife,但是我看到了示例和纯黄油刀.

P.S: I have heard about kotterknife, however I have seen an example with pure Butterknife.

我在做什么错了?

推荐答案

科特林(Kotlin)不需要黄油刀.您可以直接使用以下内容:

There's no need for butterknife in Kotlin. You can directly use the following:

//app:build.gradle文件

// app:build.gradle file

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文件

// MainActivity.kt file

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setSupportActionBar(toolbar)

        // use the kotlin property
        tvHello.text="Hi bla bla"
        tvId.text="buubububub"
        //set textcolor
        tvId.setTextColor(ContextCompat.getColor(this, R.color.colorAccent))
        etDemo.hint="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()
    }

    ...
}

对于onTextChangeListner:

For onTextChangeListner:

etText.addTextChangedListener(object : TextWatcher{
        override fun afterTextChanged(p0: Editable?) {
            TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        }

        override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        }

        override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        }

    })

这篇关于Android:Kotlin和Butterknife的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-25 18:11