Activity中或fragment时部分页面点击手机返回键无效,尤其是在页面第一次创建的时候和searchView获取焦点的时候会出现这种情况,那是因为你加载的fragement或Activity中嵌套了searchView控件的问题,虽然你可能设置了viewSearcher.clearFocus();,但是依旧焦点还是在searchView上的,因此,你需要正确的已出searchView的焦点就可以正常的退出了
我们可以吧焦点放到searchView的父布局中,每次在onResume方法中手动设置焦点归属即可,这样就放置searchView每次都会获取焦点废话不多说直接上代码
@Override
public void onResume() {
super.onResume();
handler.postDelayed(runnable, 2000);
if (viewSearcher != null) {
viewSearcher.clearFocus();
}
fouce.setFocusable(true);
fouce.setFocusableInTouchMode(true);
fouce.requestFocus();
}
<LinearLayout
android:id="@+id/fouce"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_weight="1"
android:background="@mipmap/icon_search_bg"
android:orientation="horizontal">
<android.support.v7.widget.SearchView
android:id="@+id/view_searcher"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:iconifiedByDefault="false"
app:queryBackground="@color/transparent"
app:queryHint="请输入关键字、标签"
app:searchIcon="@mipmap/ic_search_app_left" />
</LinearLayout>
</LinearLayout>
大家如果不理解可以给我留言,或者可以结合着我们的这个这个博文一起看看http://blog.csdn.net/fzkf9225/article/details/77863813