如何让搜索查看松动重点和活动的其他地方点击后崩溃 [英] how to make searchview loose focus and collapse when clicked elsewhere on activity

查看:96
本文介绍了如何让搜索查看松动重点和活动的其他地方点击后崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,我做其中搜索查看折叠和展开时,搜索界面失去和获得焦点分别。然而,在失去重心的东西只能通过两种方法实现:

In my app, i'm making a search interface in which the SearchView collapses and expands when looses and gains focus respectively. However, the loosing focus thing is only done by 2 methods:

1)。如果pressed后退按钮。

1). If back button is pressed.

2)。如果除了搜索查看主页图标点击。

2). If home icon besides the searchview is clicked.

我希望它失去焦点(因此崩溃),如果用户点击,不仅对这两件事情,但其他地方在屏幕上。它可以是任何东西,即。一个按钮或屏幕上的部分,而不在其上的任何图。我的意思是在空白屏幕上。

I want it to loose focus (and hence collapse) if the user clicks not only on these two things, but anywhere else on the screen. It can be anything viz. a button or the portion on the screen without any view on it. I mean on a blank screen.

推荐答案

嗯,我发现了以下解决方案。我用setOnTouchListener上的每个观点,不是搜索查看的一个实例崩溃的搜索查看。它的工作适合我。以下是code。

Well I found out the following solution. I used setOnTouchListener on every view that is not an instance of searchview to collapse the searchview. It worked perfect for me. Following is the code.

public void setupUI(View view) {

    if(!(view instanceof SearchView)) {

        view.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                searchMenuItem.collapseActionView();
                return false;
            }

        });
    }

    //If a layout container, iterate over children and seed recursion.
    if (view instanceof ViewGroup) {

        for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {

            View innerView = ((ViewGroup) view).getChildAt(i);

            setupUI(innerView);
        }
    }
}

是我提到的答案。

这篇关于如何让搜索查看松动重点和活动的其他地方点击后崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆