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

相关内容

热门资讯

了解透视!德州机器人代打脚本,... 了解透视!德州机器人代打脚本,微乐广西小程序开挂,指引教程(发现有挂)-哔哩哔哩德州机器人代打脚本是...
解谜透视!hhpoker辅助,... 解谜透视!hhpoker辅助,微信小程序游戏充值破解,阶段教程(有挂分享)-哔哩哔哩1、全新机制【h...
推荐透视!hhpoker真的有... 推荐透视!hhpoker真的有透视吗,微乐小程序游戏破解器苹果系统,窍要教程(有挂实锤)-哔哩哔哩1...
了解透视!hhpoker底牌透... 了解透视!hhpoker底牌透视脚本,微信小程序微乐为什么老输,模板教程(有挂存在)-哔哩哔哩1、全...
辅助透视!大菠萝789辅助器下... 辅助透视!大菠萝789辅助器下载,微乐小程序自建房辅助器,绝活教程(有挂神器)-哔哩哔哩大菠萝789...
分享透视!wpk模拟器多开,微... 分享透视!wpk模拟器多开,微信微乐游戏破解器,经验教程(的确有挂)-哔哩哔哩1、任何wpk模拟器多...
分享透视!wepoker私人局... 分享透视!wepoker私人局辅助器怎么用,微乐自建房黑科技入口在哪,策略教程(有挂功能)-哔哩哔哩...
曝光透视!pokemmo手机脚... 曝光透视!pokemmo手机脚本,微乐家乡app辅助器,操作教程(了解有挂)-哔哩哔哩1.pokem...
分享透视!wepoker辅助器... 分享透视!wepoker辅助器如何使用,手机微信小程序游戏破解器下载,烘培教程(真的有挂)-哔哩哔哩...
了解透视!拱趴大菠萝作弊方法,... 了解透视!拱趴大菠萝作弊方法,微乐四川小程序修改器,总结教程(有挂细节)-哔哩哔哩拱趴大菠萝作弊方法...