目的
使安卓软件盘上的回车键键编程搜索键,便于直接搜索。
效果图如下:

实现方案
- EditText的xml中增加:
android:imeOptions="actionSearch"
android:maxLines="1"
在较低SDK版本中,还需要增加android:singleLine="true"
,否则不会出现搜索按钮,并且仍然能够输入多行。
- 代码调用(Kotlin):
editText.setOnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
Toast.makeText(this, "哈哈", Toast.LENGTH_SHORT).show()
}
false
}
附录
参考资料:https://blog.csdn.net/liu470368500/article/details/79305191
安卓开发技术分享: https://www.jianshu.com/p/442339952f26

网友评论