android - 如何捕获 SearchView 清除按钮单击?

标签 android searchview

如何通过单击右侧的 X 按钮捕获用户单击清除 SearchView 文本的事件

我已经捕获了 onQueryTextChange 事件,但是,这是针对任何文本更改而不是针对那个 X 按钮的

最佳答案

在尝试了很多组合之后,我找到了如何在SearchView中捕捉X按钮后面的事件

以下是我的一个应用中 onCreateOptionsMenu 函数的代码 fragment 。 mSearchMenumSearchView 是全局变量。 X 实际上是一个 ID 为 search_close_btn 的 ImageView,文本区域是一个 ID 为 search_src_text

的 EditText View
@SuppressLint("NewApi")
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu items for use in the action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.country_list_activity_actions, menu);
        mSearchMenu = menu.findItem(R.id.action_search);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            // Get the SearchView and set the searchable configuration
            SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
            mSearchView = (SearchView) menu.findItem(R.id.action_search).getActionView();

            // Assumes current activity is the searchable activity
            mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
            mSearchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default

            // Get the search close button image view
            ImageView closeButton = (ImageView)mSearchView.findViewById(R.id.search_close_btn);

            // Set on click listener
            closeButton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    LoggerUtils.d(LOG, "Search close button clicked");
                    //Find EditText view
                    EditText et = (EditText) findViewById(R.id.search_src_text);

                    //Clear the text from EditText view
                    et.setText("");

                    //Clear query
                    mSearchView.setQuery("", false);
                    //Collapse the action view
                    mSearchView.onActionViewCollapsed();
                    //Collapse the search widget
                    mSearchMenu.collapseActionView();
                }
            });
        }

        // When using the support library, the setOnActionExpandListener() method is
        // static and accepts the MenuItem object as an argument
        mSearchMenu.setOnActionExpandListener(new OnActionExpandListener() {

            @Override
            public boolean onMenuItemActionExpand(MenuItem item) {
                //Nothing to do here
                LoggerUtils.d(LOG, "Search widget expand ");
                return true; // Return true to expand action view
            }

            @Override
            public boolean onMenuItemActionCollapse(MenuItem item) {
                LoggerUtils.d(LOG, "Search widget colapsed ");
                return true; // Return true to collapse action view
            }
        });

        return super.onCreateOptionsMenu(menu);
    }

关于android - 如何捕获 SearchView 清除按钮单击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24794377/

相关文章:

java - setKeyListener 将覆盖 setInputType 并更改键盘

android - ActionBarSherlock SearchView 转换错误

Android 将发布数据发送到服务器

Android ListView listSelector 不工作

android - 如何在运行时更改按钮形状样式?

android ClassNotFoundException : Didn't find class error

java - 设置搜索小部件后退按钮功能

android - 如何在 Ubuntu 9.04 中安装 Android SDK?

android - 工具栏中 SearchView 和 collapseIcon 之间的边距

android - 如何更改 android SearchView 文本