您可以通過獲取到[X]按鈕的引用,然後在其上設置一個onClick監聽做到這一點。在onClickListener中,您可以添加邏輯來隱藏鍵盤。
The code below was obtained from this answer
// Catch event on [x] button inside search view
int searchCloseButtonId = searchView.getContext().getResources()
.getIdentifier("android:id/search_close_btn", null, null);
ImageView closeButton = (ImageView) this.searchView.findViewById(searchCloseButtonId);
// Set on click listener
closeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Manage this event.
}
});
裏面onClick(View v)
你可以調用一個方法來隱藏鍵盤這樣
private void hideKeyboard(){
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
我已經發布,請檢查 –