Android/Kotlin搜索对话框键盘更改图标
创始人
2024-08-20 07:00:42
0

在搜索对话框中更改键盘图标的方法取决于您使用的搜索对话框库。下面是使用 Android 原生搜索对话框的示例代码:

  1. 在您的 activity 或 fragment 中,创建一个搜索视图:

    override fun onCreateOptionsMenu(menu: Menu?): Boolean { menuInflater.inflate(R.menu.search_menu, menu) val searchViewItem: MenuItem = menu!!.findItem(R.id.search_item) val searchView: SearchView = searchViewItem.actionView as SearchView return super.onCreateOptionsMenu(menu) }

  2. 建立您的搜索监听器,以响应键盘更改事件:

    searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener { override fun onQueryTextSubmit(query: String?): Boolean { // handle search submit action return false }

     override fun onQueryTextChange(newText: String?): Boolean {
         // handle search text change action
         return false
     }
    

    })

  3. 在您的搜索监听器中,使用下面的代码更改键盘图标:

    searchView.setOnSearchClickListener { searchView.setImeOptions(EditorInfo.IME_ACTION_SEARCH) val searchActionId: Int = searchView.context.resources.getIdentifier("android:id/search_go_btn", null, null) val searchActionButton: ImageView = searchView.findViewById(searchActionId) searchActionButton.setImageResource(R.drawable.your_new_icon) }

其中,"R.drawable.your_new_icon" 是您要更改的键盘图标的资源 ID。

请注意,这种方法只适用于 Android 5.0(API 级别 21)或更高版本。对于旧版本,您需要从搜索视图中获取 EditText,并更改其属性:

val searchEditTextId: Int = searchView.context.resources.getIdentifier("android:id/search_src_text", null, null)
val searchEditText: EditText = searchView.findViewById(searchEditTextId)
searchEditText.setCompoundDrawablesWithIntrinsicBounds(R.drawable.your_new_icon, 0, 0, 0)

再次注意,这仅适用于使用 Android

相关内容

热门资讯

科普常识!fishpoker正... 相信很多朋友都在电脑上玩过fishpoker正规吧,但是很多朋友都在抱怨用电脑玩起来不方便。为此小编...
一分钟了解!poker外挂,德... 一分钟了解!poker外挂,德扑之星软件介绍,其实是真的有挂(有挂介绍)-哔哩哔哩;德扑之星软件介绍...
透视辅助!菠萝德州app挂哪里... 透视辅助!菠萝德州app挂哪里买(透视)原来是真的有挂(详细教程)(有挂规律)-哔哩哔哩;支持2-1...
透视好牌(德州ai智能营销系统... 这是一款非常优秀的德州ai智能营销系统 ia辅助检测软件,能够让你了解到德州ai智能营销系统中牌率当...
玩家必备科技!pokerwor... 《pokerworld下载外挂软件透明挂》是一款多人竞技的pokerworld下载外挂辅助透视游戏,...
专业讨论!哈糖大菠萝拿好牌,来... 专业讨论!哈糖大菠萝拿好牌,来玩app德州安卓能用,原来真的有挂(有挂技巧)-哔哩哔哩;免费来玩ap...
玩家必看科普!pokerrrr... 玩家必看科普!pokerrrr开挂(透视)透视辅助(有挂方法)-哔哩哔哩;最新版2024是一款经典耐...
大家学习交流!来玩德州app辅... 1、大家学习交流!来玩德州app辅助工具(透视)原来是真的有挂(详细教程)(有挂教程)-哔哩哔哩。2...
热点讨论(扑克王辅助工具)辅助... 热点讨论(扑克王辅助工具)辅助透视(辅助挂)其实是真的有挂(有挂总结)-哔哩哔哩;(需添加指定薇48...
记者发布!德扑自定义房间,德扑... 1、记者发布!德扑自定义房间,德扑之星清楚数据,果真真的有挂(有挂介绍)-哔哩哔哩;详细教程。2、德...