要做输入法软键盘搜索其实是挺简单的,在xml布局文件里这样设置编辑框:
<LinearLayout
android:id="@+id/lv_goto_search_goods"
android:layout_width="0dp"
android:layout_height="@dimen/dp_27"
android:layout_marginBottom="5dp"
android:layout_marginLeft="@dimen/dp_8"
android:layout_marginRight="20dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="@+id/iv_goods_list_back1"
android:layout_weight="1"
android:background="@drawable/shape_bg_solid_grey_rectangle"
android:focusable="true"
android:focusableInTouchMode="true">
<ImageView
android:layout_width="@dimen/dp_18"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="@dimen/dp_15"
android:layout_marginRight="3dp"
android:src="@drawable/ic_search_sort" />
<EditText
android:id="@+id/search_text_view1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/dp_3"
android:layout_weight="1"
android:singleLine="true"
android:cursorVisible="true"
android:imeOptions="actionSearch"
android:background="@null"
android:gravity="center_vertical"
android:hint="@string/search_goods"
android:maxLines="1"
android:textColor="#788F9B"
android:textSize="@dimen/dp_13" />
</LinearLayout>
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,就这样了。