要做输入法软键盘搜索其实是挺简单的,在xml布局文件里这样设置编辑框:

                          

android:focusable="true"
android:focusableInTouchMode="true"
这两个属性一定要在编辑框的父布局上,用于屏蔽EditText自动获取焦点问题,然后在EditText上加入
android:imeOptions="actionSearch"
android:singleLine="true"
即可改变软键盘的回车变成搜索按钮或者搜索汉字

但是,搜索的逻辑你可能会这样写:

 search_text_view1.setOnEditorActionListener { v, actionId, event ->            //修改回车键功能            if (actionId === EditorInfo.IME_ACTION_SEARCH || (event != null && event.keyCode == KeyEvent.KEYCODE_ENTER)) {                // 隐藏键盘                (getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager)                        .hideSoftInputFromWindow(this@GoodsSelecotorActivity.currentFocus                                .windowToken, InputMethodManager.HIDE_NOT_ALWAYS)                keywords = search_text_view1.text.toString()                currentPage = 1                getRequestData(promotionId)            }            false        }

或者

 search_text_view1.setOnKeyListener { v, actionId, event ->            //修改回车键功能            if (actionId === EditorInfo.IME_ACTION_SEARCH || (event != null && event.keyCode == KeyEvent.KEYCODE_ENTER)) {                // 隐藏键盘                (getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager)                        .hideSoftInputFromWindow(this@GoodsSelecotorActivity.currentFocus                                .windowToken, InputMethodManager.HIDE_NOT_ALWAYS)                keywords = search_text_view1.text.toString()                currentPage = 1                getRequestData(promotionId)            }            false        }

那么恭喜你,你已经掉坑了:搜索方法会执行两次

其实,当你调用setOnKeyListener的时候已经进坑了,这个方法是会执行两次的,就会导致你的网络请求执行两次,那么要么是加载弹窗一直显示,要么是数据重复了,正确的做法是放弃该方法,调用setOnEditorActionListener ,但是这个里面也要做一些修改才行,

 search_text_view1.setOnEditorActionListener { v, actionId, event ->            //修改回车键功能            if (actionId === EditorInfo.IME_ACTION_SEARCH) {                // 隐藏键盘                (getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager)                        .hideSoftInputFromWindow(this@GoodsSelecotorActivity.currentFocus                                .windowToken, InputMethodManager.HIDE_NOT_ALWAYS)                keywords = search_text_view1.text.toString()                currentPage = 1                getRequestData(promotionId)            }            false        }

对,就是这样,if判断力精简了
if (actionId === EditorInfo.IME_ACTION_SEARCH) {
//正常逻辑
}
有的时候,加的判断多了,反而适得其反,Ok,就这样了。

更多相关文章

  1. 软键盘显示及属性android:windowSoftInputMode使用总结 & [转一
  2. Android 5.1部分apk的升级或缓存功能无效
  3. Android知识点——更改软键盘回车键
  4. android 将鼠标右键点击事件改为点击后返回功能
  5. Android 点击空白或滑动时候关闭软键盘(有scrollview的坑)
  6. Android 无障碍辅助功能AccessibilityService(2)
  7. Android Lollipop 5.0 新功能说明
  8. Android APP禁止旋转和软键盘的控制
  9. android:imeOptions属性详解(Enter功能)

随机推荐

  1. Android(安卓)RecyclerView 间距全适配
  2. Android(安卓)- 禁止Gridview滚动
  3. android 程序漰溃 后台handle处理类
  4. android 网络下载获取文件大小
  5. 封装Android(安卓)OKHttp3.0请求工具
  6. Android全屏和强制横屏竖屏设置
  7. android RadioGroup设置某一个被选中
  8. Android(安卓)1.5 自带的图标一览表
  9. android时间控件DatePicker使用实例
  10. Android(安卓)ContextMenu上下文菜单