如何完成构造函数?

时间:2017-07-25 16:39:37

标签: android constructor kotlin

我有这段代码:

override fun onCreateOptionsMenu(menu: Menu): Boolean {
        menuInflater.inflate(R.menu.menu_search, menu)
        val searchItem = menu.findItem(R.id.action_search)
        val searchView = MenuItemCompat.getActionView(searchItem) as SearchView
        //*** setOnQueryTextFocusChangeListener ***
        searchView.setOnQueryTextFocusChangeListener(object : View.OnFocusChangeListener() {

            override fun onFocusChange(v: View, hasFocus: Boolean) {

            }
        })

        searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {

            override fun onQueryTextSubmit(query: String): Boolean {

                return false
            }

            override fun onQueryTextChange(searchQuery: String): Boolean {
                adapter!!.filter(searchQuery.toString().trim { it <= ' ' })
                tvListAnimal.invalidate()
                return true
            }
        })


        return true
    }

问题来自这部分:

searchView.setOnQueryTextFocusChangeListener(object : View.OnFocusChangeListener() {

   override fun onFocusChange(v: View, hasFocus: Boolean) {

   }
})

当我尝试使用Android Studio运行时,收到此消息:

  

&#39;此课程没有构造函数。&#39;

我试着这样写:

searchView.setOnQueryTextFocusChangeListener(object : View.OnFocusChangeListener {

   override fun onFocusChange(v: View, hasFocus: Boolean) {

   }
})

虽然有效,但应用程序崩溃,因为我在View.OnFocusChangeListener之后删除了括号。

我不知道如何完成这个。有人有想法吗?

0 个答案:

没有答案