使用Lambda创建回调 [英] Create a callback using a lambda
问题描述
我在尝试找出如何使用lambdas在Kotlin中创建回调的过程中遇到了困难.我有一个自定义的TextInputEditText,我想实现一个功能,当文本更改时,活动可以调用该功能.
I'm having a difficult time trying how to figure out how to create a callback in Kotlin using lambdas. I have a custom TextInputEditText and I want to implement a function that the activity can call when text changes.
这是我自定义的EditText:
Here is my custom 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事件时调用的回调.自定义控件中的回调仅将文本发送回客户端.所以在我的活动中,我想要这样的东西:
In my activity I want to have a callback that gets called when the onTextChanged event gets called. The callback in the custom control sends only the text back to the client. So in my activity, I want something like this:
editText.onTextAvailable(text -> do something )
推荐答案
除了@EpicPandaForce提供的解决方案外,还有其他一些解决方案.如果您要坚持使用示例中所示的类,则可以执行以下操作:
In addition to the solution by @EpicPandaForce, there are a couple other solutions. If you want to stick with using a class as you've shown in your example, then you can do this:
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
}
}
然后在您的活动中创建一个函数:
Then in your activity create a function:
fun onTextChange(text: String) {
// Do something with the text.
}
然后按如下所示设置您的回调:
And then setup your callback as follows:
my_edittext.onTextChange(::onTextChange)
此解决方案使您可以将相同的onTextChange函数重用于其他也要使用它的控件.
This solution allows you to re-use the same onTextChange function for other controls that want to use it as well.
如果您希望使用接口来定义回调,请执行以下操作:
If you prefer to use an interface to define the callback, do this:
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
}
}
然后在您的活动中,按如下所示创建回调:
Then in your activity, create the callback as follows:
val textChangeHandler = object: ITextWatcher {
override fun onTextChanged(text: String) {
var t = text
}
}
然后为您的edittext控件设置回调,如下所示:
And then setup your callback for your edittext controls as follows:
my_edittext.onTextChange(textChangeHandler)
这篇关于使用Lambda创建回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!