自动折叠动作条搜索查看软键盘关闭 [英] Auto Collapse ActionBar SearchView on Soft Keyboard close

查看:138
本文介绍了自动折叠动作条搜索查看软键盘关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用的动作条菜单项,正在以显示操作栏搜索查看。当搜索菜单项被扩大了软键盘显示这正是我想要的。现在,当用户presses后退按钮以关闭软键盘,我也要崩溃了搜索查看操作栏中。

I am currently using an ActionBar menu item to display a SearchView in the action bar. When the search menu item is expanded the soft keyboard is displayed which is what I want. Now, when the user presses the back button to close the soft keyboard, I would also like to collapse the SearchView in the action bar.

我曾尝试实施以下听众OnKeyListener和OnFocusChangeListener的菜单项和而ActionView。我也试图在活动使用OnBack pressed()。以上都不是检测时的后退按钮用来关闭软键盘。

I have tried implementing the following listeners OnKeyListener and OnFocusChangeListener on the MenuItem and the ActionView. I have also tried using OnBackPressed() in the Activity. None of the above detect when the back button is used to close the soft keyboard.

任何想法?

我已经实现OnActionExpandListener知道什么时候搜索查看可见。

I have implemented OnActionExpandListener to know when the SearchView is visible.

推荐答案

我会在@ user1258568的懒惰答案扩大。这为我工作。请注意,它会清除您的查询时,焦点将丢失。

I'll expand on @user1258568 's answer for the lazy. This worked for me. Note that it clears your query when focus is lost.

final MenuItem searchMenuItem = optionsMenu.findItem(R.id.search);
final SearchView searchView = (SearchView) searchMenuItem.getActionView();

searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View view, boolean queryTextFocused) {
        if(!queryTextFocused) {
            searchMenuItem.collapseActionView();
            searchView.setQuery("", false);
        }
    }
});

这篇关于自动折叠动作条搜索查看软键盘关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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