解决Activity中或fragment时,点击手机返回键无效,无法触发onKeyDown

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

青丶穗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值