隐藏 ActionBar 菜单时 SearchView 聚焦

发布于 2024-12-07 04:48:07 字数 1275 浏览 5 评论 0原文

我有一个带有 SearchView 的 ActionBar。当选择菜单按钮时,隐藏/溢出菜单项将显示在右侧的下拉列表中。当下拉菜单隐藏时,SearchView 将获得焦点并显示键盘。有没有办法阻止键盘显示(触摸 SearchView 的情况除外)?

问候, 朱利叶斯.

编辑下面添加的代码: 以下是我初始化它的方法:

        SearchManager searchManager = (SearchManager) mActivity.getSystemService(Context.SEARCH_SERVICE);
        ((SearchView) mSearchView).setSearchableInfo(searchManager.getSearchableInfo(mActivity.getComponentName()));

        mSearchView.setIconifiedByDefault(mIconified);
        mSearchView.setOnQueryTextListener(this);
        mSearchView.setOnCloseListener(this);
        mSearchView.setFocusable(false);
        mSearchView.setFocusableInTouchMode(false);

        if(null!=mQuery)
            mSearchView.setQuery(mQuery, false);

编辑2:

这是当用户想要开始搜索时我所做的:

    @Override
    public boolean onQueryTextSubmit(String query) {
        // Hide keyboard
        InputMethodManager imm = (InputMethodManager) mActivity.getSystemService(
                FragmentActivity.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(mSearchView.getWindowToken(), 0);

...

        mSearchView.setFocusable(false);
        mSearchView.setFocusableInTouchMode(false);

        return true;
    }

I have an ActionBar with a SearchView. The hidden/overflow menu items are shown on the right in a drop-down list when the menu button is selected. When the drop-down menu is hidden, the SearchView is focused and the keyboard shows. Is there a way to stop the keyboard from showing (except for the case where the SearchView is touched)?

Regards,
Julius.

Edit added code below:
Here is how I initialise it:

        SearchManager searchManager = (SearchManager) mActivity.getSystemService(Context.SEARCH_SERVICE);
        ((SearchView) mSearchView).setSearchableInfo(searchManager.getSearchableInfo(mActivity.getComponentName()));

        mSearchView.setIconifiedByDefault(mIconified);
        mSearchView.setOnQueryTextListener(this);
        mSearchView.setOnCloseListener(this);
        mSearchView.setFocusable(false);
        mSearchView.setFocusableInTouchMode(false);

        if(null!=mQuery)
            mSearchView.setQuery(mQuery, false);

Edit 2:

Here is what I do when the user wants to start the search:

    @Override
    public boolean onQueryTextSubmit(String query) {
        // Hide keyboard
        InputMethodManager imm = (InputMethodManager) mActivity.getSystemService(
                FragmentActivity.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(mSearchView.getWindowToken(), 0);

...

        mSearchView.setFocusable(false);
        mSearchView.setFocusableInTouchMode(false);

        return true;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

南七夏 2024-12-14 04:48:07

尝试在 searchView 初始化时调用 mSearchView.clearFocus() 。当我遇到类似问题时,这对我有用。

try calling mSearchView.clearFocus() on your initialization of the searchView. That worked for me when I had a similar problem.

盗心人 2024-12-14 04:48:07

尝试在初始化 SearchView 时调用 setFocusable(false) 。

try call setFocusable(false) on SearchView when you init it.

笔落惊风雨 2024-12-14 04:48:07

我在片段中创建选项菜单时实例化了搜索视图,但上述方法都不适合我。

但是,我能够通过保留对 SearchView 的引用来解决问题,而不是在 onCreateOptionsMenu() 中设置 SearchView 的图标化设置,而是在片段恢复时将其设置在帖子上,如下所示:

@Override
public void onResume() {
    super.onResume();
    getView().post(new Runnable() {
        public void run() {
            if (mFilterView.isIconfiedByDefault()) {
                mFilterView.setIconifiedByDefault(false);
                mFilterView.clearFocus();
            }
        }
    });
}

I instantiated a search view when creating the options menu in a fragment and none of the above methods worked for me.

However, I was able to solve the problem by holding a reference to the SearchView, NOT setting the SearchView's iconified setting in onCreateOptionsMenu(), but setting it on a post when the fragment resumed like follows:

@Override
public void onResume() {
    super.onResume();
    getView().post(new Runnable() {
        public void run() {
            if (mFilterView.isIconfiedByDefault()) {
                mFilterView.setIconifiedByDefault(false);
                mFilterView.clearFocus();
            }
        }
    });
}
似狗非友 2024-12-14 04:48:07

调用 requestFocus() 以获得另一个视图,最好是您的结果视图,例如

_resultsFragment.getView().requestFocus();

Call requestFocus() for another view, ideally your results view, e.g.

_resultsFragment.getView().requestFocus();
时常饿 2024-12-14 04:48:07

我刚刚因为同样的问题而烦恼不已。

我尝试过 android:focusable="false"。我尝试了输入法管理器。我尝试创建一个虚拟视图来获取焦点,但没有任何效果。

我最终在 onCreateOptionsMenu() 中使用以下代码解决了这个问题:

final ViewTreeObserver observer = mSearchView.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener(){
    @Override
    public void onGlobalLayout() {
        observer.removeOnGlobalLayoutListener(this);
        mSearchView.clearFocus();
    }
});

问题是在 Fragment 生命周期的每个点上,菜单都尚未创建。 onCreateOptionsMenu() 方法是在 onResume() 之后调用的,但仅在我测试过的某些设备上调用,这真的很奇怪。

上面的代码确保在页面创建期间发生任何焦点更改后清除焦点。

我希望这对其他人有帮助,从一开始就知道这一点会节省我很多时间。

I have just been pulling my hair out over the same issue.

I tried android:focusable="false". I tried the InputMethodManager. I tried creating a dummy view to take the focus but nothing worked.

I finally solved it with the following code inside onCreateOptionsMenu():

final ViewTreeObserver observer = mSearchView.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener(){
    @Override
    public void onGlobalLayout() {
        observer.removeOnGlobalLayoutListener(this);
        mSearchView.clearFocus();
    }
});

The issue was that at every point in the Fragment's lifecycle the menu had not been created yet. The onCreateOptionsMenu() method was being called after onResume(), but only on some devices I tested on, which is really strange.

The above code makes sure that clearing the focus is done after any focus changes during creation of the page.

I hope this helps someone else, it would have saved me a lot of time to know this from the start.

缱绻入梦 2024-12-14 04:48:07

当您在 XML 布局文件中设置 SearchView 时(如果您正在使用一个),只需使用以下方法:

android:focusable="false"

通过这种方法,您的 SearchView 在触摸它之前不会获得焦点...无论在何处或是否“初始化” " 将其添加到代码中,或者隐藏菜单,或者调试中可能未发生的任何其他活动。这也消除了跟踪您的呼叫位置的可能需要

setFocusable(false) //本质上做同样的事情

setFocusable(false) //本质上按照接受的答案所建议的那样在代码中多次

When you set up your SearchView in your XML layout file (if you're using one), just use this:

android:focusable="false"

With this approach, your SearchView won't gain focus until you touch it...no matter where or if you "init" it in your code, or you hide your menu, or any other activity that might not have occurred in your debugging. This also eliminates the possible need to track where you are calling

setFocusable(false) //essentially does the same thing

in your code multiple times as the accepted answer suggests.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文