所以我有这个 Java 代码:
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
doSomething();
return true;
}
return false;
}
});
我已经设法得到这个(我什至不确定这是正确的方法):
editText.setOnEditorActionListener() { v, actionId, event ->
if(actionId == EditorInfo.IME_ACTION_DONE){
doSomething()
} else {
}
}
但是我得到一个错误 Error:(26, 8) Type mismatch: inferred type is kotlin.Unit but kotlin.Boolean was expected
那么这样的事件处理程序是如何用 Kotlin 编写的呢?
原文由 Pier 发布,翻译遵循 CC BY-SA 4.0 许可协议
onEditorAction
返回一个Boolean
而你的 Kotlin lambda 返回Unit
。将其更改为:关于 lambda 表达式和匿名函数 的文档 是一本很好的读物。