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

编程入门 行业动态 更新时间:2024-10-11 15:18:05
本文介绍了如何捕获SearchView的清除按钮单击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

How can I capture the event of user click on clear SearchView text by clicking on the X button on the right

我已经捕获了onQueryTextChange事件,但这是针对所有文本更改的,而不是针对该X按钮的

I already captured onQueryTextChange event but, this is for any text change not for that X button

推荐答案

尝试了多种组合之后,我发现了如何捕获SearchView中X按钮后面的事件

After trying a lot of combinations, I found how to capture the event behind the X button in SearchView

下面是我的一个应用程序中onCreateOptionsMenu函数的代码片段. mSearchMenu和mSearchView是全局变量. X实际上是ID为search_close_btn的ImageView,文本区域为ID为search_src_text

Below is a code snippet from onCreateOptionsMenu function in one of my apps. mSearchMenu and mSearchView are global variables. The X is actually an ImageView with ID search_close_btn and the text area is an EditText view with ID search_src_text

@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); }

更多推荐

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

本文发布于:2023-11-11 05:26:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1577503.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单击   按钮   SearchView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!