使用Lambda创建回调

时间:2019-06-14 17:38:55

标签: android kotlin lambda

我很难解决如何使用lambda在Kotlin中创建回调的问题。我有一个自定义的TextInputEditText,我想实现一个功能,当文本更改时,活动可以调用该功能。

这是我自定义的EditText:

class EditTextEx : TextInputEditText, TextWatcher {
    constructor(context: Context?) : super(context)
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)


    override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
      // Call the callback onTextAvailable with the EditText's text (s.toString)
    }

    override fun afterTextChanged(p0: Editable?) {
    }

    override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
    }

}

在我的活动中,我希望有一个在调用onTextChanged事件时调用的回调。自定义控件中的回调仅将文本发送回客户端。所以在我的活动中,我想要这样的东西:

editText.onTextAvailable(text -> do something )

3 个答案:

答案 0 :(得分:1)

这实际上很容易做到,

Execute Package Task

现在您可以打电话

inline fun EditText.onTextChanged(crossinline onTextChange: (String) -> Unit): TextWatcher {
    val textWatcher = object: TextWatcher {
        override fun afterTextChanged(editable: Editable) {
            onTextChange(editable.toString())
        }

        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
    }
    this.addTextChangeListener(textWatcher)
    return textWatcher
}

答案 1 :(得分:0)

尝试这样的事情:

fun getEditTextChange(editText: EditText, onTextChange: (String) -> Unit){
        val tw = object: TextWatcher {
            private var region = Locale.getDefault().language

            override fun afterTextChanged(s: Editable?) {
                onTextChange.invoke(s.toString())
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}

        }
        editText.addTextChangedListener(tw)
    }

希望有帮助

答案 2 :(得分:0)

除了@EpicPandaForce的解决方案外,还有其他两种解决方案。如果您想坚持使用示例中所示的类,则可以执行以下操作:

class EditTextEx : TextInputEditText, TextWatcher {
    constructor(context: Context?) : super(context)
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)

    private var mOnTextWatcherCallback: (m: String) -> Unit = {}

    override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
        if (mOnTextWatcherCallback != null)
            mOnTextWatcherCallback(s.toString())
    }

    override fun afterTextChanged(p0: Editable?) {
    }

    override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
    }

    fun onTextChange(callback: (text: String) -> Unit) {
        mOnTextWatcherCallback = callback
    }
}

然后在您的活动中创建一个函数:

fun onTextChange(text: String) {
  // Do something with the text.
}

然后按如下所示设置您的回调:

my_edittext.onTextChange(::onTextChange)

此解决方案使您可以将相同的onTextChange函数重新用于要使用它的其他控件。

如果您希望使用接口来定义回调,请执行以下操作:

class EditTextEx : TextInputEditText, TextWatcher {
    constructor(context: Context?) : super(context)
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)

    private var mOnTextWatcherCallback: ITextWatcher? = null

    override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
        if (mOnTextWatcherCallback != null)
            mOnTextWatcherCallback!!.onTextChanged(s.toString())
    }

    override fun afterTextChanged(p0: Editable?) {
    }

    override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
    }

    fun onTextChange(callback: ITextWatcher) {
        mOnTextWatcherCallback = callback
    }
}

然后在您的活动中,如下创建回调:

val textChangeHandler = object: ITextWatcher {
    override fun onTextChanged(text: String) {
        var t = text
    }
}

然后为您的edittext控件设置回调,如下所示:

my_edittext.onTextChange(textChangeHandler)