Android 两个类似的代码构造,但其中一个不起作用
下面是两段基本上应该做相同事情的代码。但是第二个不执行onEditorAction,而第一个执行。第二个不同之处是什么阻止它执行代码?注意:代码中只存在其中一个,而不是两个Android 两个类似的代码构造,但其中一个不起作用,android,kotlin,Android,Kotlin,下面是两段基本上应该做相同事情的代码。但是第二个不执行onEditorAction,而第一个执行。第二个不同之处是什么阻止它执行代码?注意:代码中只存在其中一个,而不是两个 // This one works this.setOnEditorActionListener { v, actionId, event -> if(actionId == EditorInfo.IME_ACTION_SEARCH){ mOnRunSearchCallb
// 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和event的参数类型是错误的。v和event都可以为null。你是对的。我想您不能完全依赖于从Java到Kotlin的复制/粘贴。我粘贴了stackoverflow解决方案的Java版本。请注意,我更喜欢较短的版本。也不需要添加问号来表示可以为null。