android - 两个相似的代码结构,但一个不起作用
问题描述
以下是本质上应该做同样事情的两个代码块。但是第二个不执行 onEditorAction 而第一个执行。阻止它执行代码的第二个不同之处是什么?注意:代码中只有其中一个存在,而不是两者都存在。
// This one works
this.setOnEditorActionListener { v, actionId, event ->
if(actionId == EditorInfo.IME_ACTION_SEARCH){
mOnRunSearchCallback()
true
} else {
false
}
}
// This one does not work
this.setOnEditorActionListener(object : TextView.OnEditorActionListener {
override fun onEditorAction(v: TextView, actionId: Int, event: KeyEvent): Boolean {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
mOnRunSearchCallback()
return true
}
return false
}
})
解决方案
用这个改变第二个例子
this.setOnEditorActionListener(object : TextView.OnEditorActionListener {
override fun onEditorAction(v: TextView?, actionId: Int, event: KeyEvent?): Boolean {
return true;
}
})
基本上,你有错误的参数类型 v 和事件v
&event
都是可以为空的。
推荐阅读
- asp.net-mvc - UserManager.UpdateSecurityStamp reverts user data
- java - 我如何遍历对象的 LinkedHashSet?
- php - if else 怎么写
- jenkins - 如何重启在windows机器上运行的jenkins
- c++ - C++ 获得“第二个命令行参数”
- sql - 我想按列显示列值组的总和。如何显示?
- visual-studio-code - Search selected text in all workspace files without having to hit the enter key to actually start the search?
- javafx - JavaFXML 创建新场景
- git - Yocto throws a git Fatal Error "Please tell me who you are" after after successful Bitbake build
- android - 当外部库中抛出异常时,Android Studio 挂起