方案一 : 使用输入法管理器进行隐藏
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
getWindowToken()的方式还有其他几种, 如:
etSearch.getWindowToken() //用控件对象来获取
activity.getCurrentFocus().getWindowToken() //可能会空指针异常
是否有效需要具体测试, 本人在几个项目中用过这个方案, 有时就无法隐藏
方案二 : 让其他控件获取焦点
tvSearch.requestFocus();
该方案本人测试过两次, 一次有效一次无效
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="true">
<EditText
android:id="@+id/et_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/iv_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:src="@drawable/selector_btn_search"/>
</RelativeLayout>