在Android中搜索SearchView的UP图标上的Click Event。

时间:2014-09-09 05:44:51

标签: android android-actionbar android-search

我已在 SearchView 上实施了 ListView 。当我在此搜索小部件的 EditText 中输入一些文字时,它会过滤我列表中的内容。但是,如果我在点击 UP/home 图标时关闭此搜索小部件而未清除 EditText 中的文字,我仍然会将过滤后的列表设置为不完整名单。

所以我需要抓住 UP SearchView 图标,以清除 EditText中的文字,或者还有其他方法。

enter image description here

2 个答案:

答案 0 :(得分:2)

可能是,您需要尝试使用此功能,而不是依赖 UP 图标点击事件。

menuItem.setOnActionExpandListener(new OnActionExpandListener()
        {

            @Override
            public boolean onMenuItemActionCollapse(MenuItem item)
            {
                // Do something when collapsed

                return true; // Return true to collapse action view
            }

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

答案 1 :(得分:0)

你就是这样做的。

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()){
    // this is item ID of that back part which you want
    case android.R.id.home:
          // Control will come here when back/or app icon is pressed
    break;
    }
    return super.onOptionsItemSelected(item);
}