如何处理 SearchView 中的后退箭头事件 [英] How to handle back arrow event in a SearchView

查看:49
本文介绍了如何处理 SearchView 中的后退箭头事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何处理搜索视图小部件中后退箭头上的点击事件:

How can I handle the click event on the back arrow in the searchview widget:

我试过这段代码,但它不起作用:

I tried this code but it doesn't work:

searchtollbar.setNavigationOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Log.d(TAG, "back arrow clicked");
    }

});

我也试过这个:

MenuItemCompat.setOnActionExpandListener(item_search, new 
MenuItemCompat.OnActionExpandListener() {
    @Override
    public boolean onMenuItemActionCollapse(MenuItem item) {
        // Do something when collapsed
        return true;
    }

    @Override
    public boolean onMenuItemActionExpand(MenuItem item) {
        // Do something when expanded
        return true;
    }
});

上述代码的问题在于,调用 onMenuItemActionCollapse() 方法也会执行 onQueryTextChange(),这是不可取的.

The problem with the above code is that calling onMenuItemActionCollapse() method also executes onQueryTextChange() which is undesirable.

所以请帮忙.

推荐答案

要处理您必须重写 onOptionsItemSelected 方法.

To handle that you have to override onOptionsItemSelected method.

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case android.R.id.home:
                // handle back event.
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }

这篇关于如何处理 SearchView 中的后退箭头事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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