要做輸入法軟鍵盤(pán)搜索其實(shí)是挺簡(jiǎn)單的揍堕,在xml布局文件里這樣設(shè)置編輯框:
<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"
這兩個(gè)屬性一定要在編輯框的父布局上竞慢,用于屏蔽EditText自動(dòng)獲取焦點(diǎn)問(wèn)題,然后在EditText上加入
android:imeOptions="actionSearch"
android:singleLine="true"
即可改變軟鍵盤(pán)的回車(chē)變成搜索按鈕或者搜索漢字
但是织狐,搜索的邏輯你可能會(huì)這樣寫(xiě):
search_text_view1.setOnEditorActionListener { v, actionId, event ->
//修改回車(chē)鍵功能
if (actionId === EditorInfo.IME_ACTION_SEARCH || (event != null && event.keyCode == KeyEvent.KEYCODE_ENTER)) {
// 隱藏鍵盤(pán)
(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 ->
//修改回車(chē)鍵功能
if (actionId === EditorInfo.IME_ACTION_SEARCH || (event != null && event.keyCode == KeyEvent.KEYCODE_ENTER)) {
// 隱藏鍵盤(pán)
(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
}
那么恭喜你暂幼,你已經(jīng)掉坑了:搜索方法會(huì)執(zhí)行兩次
其實(shí),當(dāng)你調(diào)用setOnKeyListener的時(shí)候已經(jīng)進(jìn)坑了移迫,這個(gè)方法是會(huì)執(zhí)行兩次的旺嬉,就會(huì)導(dǎo)致你的網(wǎng)絡(luò)請(qǐng)求執(zhí)行兩次,那么要么是加載彈窗一直顯示厨埋,要么是數(shù)據(jù)重復(fù)了邪媳,正確的做法是放棄該方法,調(diào)用setOnEditorActionListener 荡陷,但是這個(gè)里面也要做一些修改才行雨效,
search_text_view1.setOnEditorActionListener { v, actionId, event ->
//修改回車(chē)鍵功能
if (actionId === EditorInfo.IME_ACTION_SEARCH) {
// 隱藏鍵盤(pán)
(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
}
對(duì),就是這樣废赞,if判斷力精簡(jiǎn)了
if (actionId === EditorInfo.IME_ACTION_SEARCH) {
//正常邏輯
}
有的時(shí)候设易,加的判斷多了,反而適得其反蛹头,Ok顿肺,就這樣了。