Android: Kotlin with Butterknife

前端 未结 10 987
鱼传尺愫
鱼传尺愫 2020-12-24 04:55

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

Here is my build.gradle

dependencies {
    ...
    compile \'com.jakewharton:b         


        
10条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-24 05:30

    You can implement some extensions to improve your views behavior. Checkout this example for "onTextChange" in a regular editText:

    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)
            }
        })
    }
    

    Usage:

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

    I vote for kotlin-android-extensions

提交回复
热议问题