首页 > 解决方案 > Kotlin - 未调用 google 的 Place Autocomplete API onClick 的清除按钮侦听器

问题描述

我正在尝试为来自谷歌的 Place Autocomplete API 的清除按钮创建一个侦听器。clearButton()我在片段的方法中调用了我的onViewCreated方法

清除按钮()

placeAutocompleteFragment?.view?.findViewById<View>(R.id.place_autocomplete_clear_button)
    ?.setOnClickListener {
        View.OnClickListener {
            Log.d(TAG, "Cleared")
            it?.findViewById<EditText>(R.id.place_autocomplete_search_input)?.setText("")
            it?.visibility = View.GONE
        }
    }

现在,当我单击清除按钮图标时,文本不会被删除,没有任何反应。我仍然可以输入一个新的位置,但我无法清除它。我Log.d的也没有显示。

标签: androidkotlingoogle-places-apionclicklistenertype-inference

解决方案


弄清楚了。我把方法调用设置错了。

它应该是这样的:

private fun clearButton() {
    placeAutocompleteFragment?.view?.findViewById<View>(R.id.place_autocomplete_clear_button)?.setOnClickListener {
        Log.d(TAG, "Cleared Button Clicked")
        it.visibility = View.GONE
        //do something
    }
}

推荐阅读