两种类似的代码构造,但一种不起作用

时间:2019-06-25 08:20:01

标签: android kotlin

以下是本质上应该做相同事情的两个代码块。但是第二个不执行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
        }
})

1 个答案:

答案 0 :(得分:1)

以此更改第二个示例

this.setOnEditorActionListener(object : TextView.OnEditorActionListener {
      override fun onEditorAction(v: TextView?, actionId: Int, event: KeyEvent?): Boolean {
             return true;
      }
 })

基本上,您为v使用了错误的参数类型,并且vevent都可以为空。