How can I capture the event of user click on clear SearchView
text by clicking on the X button on the right
如何通过单击右边的X按钮来捕获用户单击clear SearchView文本的事件
I already captured onQueryTextChange
event but, this is for any text change not for that X button
我已经捕获了onQueryTextChange事件,但是,这是对于任何文本更改而不是那个X按钮。
6 个解决方案
#1
76
After trying a lot of combinations, I found how to capture the event behind the X button in SearchView
在尝试了许多组合之后,我发现如何在SearchView中捕获X按钮后面的事件
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
下面是我的一个应用程序中onCreateOptionsMenu函数的代码片段。mSearchMenu和mSearchView是全局变量。X实际上是ID search_close_btn的ImageView,文本区域是ID search_src_text的EditText视图
@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);
}
#2
42
you can just use the onCloseListener()
你可以使用onCloseListener()
sv= (SearchView) findViewById(R.id.searchView1);
sv.setOnCloseListener(new OnCloseListener() {
@Override
public boolean onClose() {
Toast t = Toast.makeText(MainActivity.this, "close", Toast.LENGTH_SHORT);
t.show();
return false;
}
});
#3
28
I had problems trying to find the component by its ID but I found another way to search this component using the context of the same SearchView
我在试图通过它的ID查找组件时遇到了问题,但是我找到了另一种使用相同SearchView的上下文搜索这个组件的方法
// Catch event on [x] button inside search view
int searchCloseButtonId = searchView.getContext().getResources()
.getIdentifier("android:id/search_close_btn", null, null);
ImageView closeButton = (ImageView) this.searchView.findViewById(searchCloseButtonId);
// Set on click listener
closeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Manage this event.
}
});
#4
19
I use this code to catch text query clearing, and perform my actions
我使用此代码捕获文本查询清理,并执行我的操作
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search, menu);
SearchView searchView = (SearchView) menu.findItem(R.id.search_button).getActionView();
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
SearchView.OnQueryTextListener textChangeListener = new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextChange(String cs) {
if (TextUtils.isEmpty(cs)){
//Text is cleared, do your thing
}
return false;
}
@Override
public boolean onQueryTextSubmit(String query) {
//text query submitted
}
};
searchView.setOnQueryTextListener(textChangeListener);
return true;
}
#5
3
If you use Appcompat library, instead of using
如果您使用Appcompat库,而不是使用
getResources().getIdentifier("android:id/search_close_btn", null, null);
you can do this:
你可以这样做:
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) searchItem.getActionView();
View closeButton = searchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
closeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//handle click
}
});
#6
2
ImageView closeButton = (ImageView) this.searchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
closeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Manage this event.
}
});
Use R.id.search_go_btn for "Submit" Button
使用R.id。search_go_btn“提交”按钮
#1
76
After trying a lot of combinations, I found how to capture the event behind the X button in SearchView
在尝试了许多组合之后,我发现如何在SearchView中捕获X按钮后面的事件
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
下面是我的一个应用程序中onCreateOptionsMenu函数的代码片段。mSearchMenu和mSearchView是全局变量。X实际上是ID search_close_btn的ImageView,文本区域是ID search_src_text的EditText视图
@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);
}
#2
42
you can just use the onCloseListener()
你可以使用onCloseListener()
sv= (SearchView) findViewById(R.id.searchView1);
sv.setOnCloseListener(new OnCloseListener() {
@Override
public boolean onClose() {
Toast t = Toast.makeText(MainActivity.this, "close", Toast.LENGTH_SHORT);
t.show();
return false;
}
});
#3
28
I had problems trying to find the component by its ID but I found another way to search this component using the context of the same SearchView
我在试图通过它的ID查找组件时遇到了问题,但是我找到了另一种使用相同SearchView的上下文搜索这个组件的方法
// Catch event on [x] button inside search view
int searchCloseButtonId = searchView.getContext().getResources()
.getIdentifier("android:id/search_close_btn", null, null);
ImageView closeButton = (ImageView) this.searchView.findViewById(searchCloseButtonId);
// Set on click listener
closeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Manage this event.
}
});
#4
19
I use this code to catch text query clearing, and perform my actions
我使用此代码捕获文本查询清理,并执行我的操作
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search, menu);
SearchView searchView = (SearchView) menu.findItem(R.id.search_button).getActionView();
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
SearchView.OnQueryTextListener textChangeListener = new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextChange(String cs) {
if (TextUtils.isEmpty(cs)){
//Text is cleared, do your thing
}
return false;
}
@Override
public boolean onQueryTextSubmit(String query) {
//text query submitted
}
};
searchView.setOnQueryTextListener(textChangeListener);
return true;
}
#5
3
If you use Appcompat library, instead of using
如果您使用Appcompat库,而不是使用
getResources().getIdentifier("android:id/search_close_btn", null, null);
you can do this:
你可以这样做:
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) searchItem.getActionView();
View closeButton = searchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
closeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//handle click
}
});
#6
2
ImageView closeButton = (ImageView) this.searchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
closeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Manage this event.
}
});
Use R.id.search_go_btn for "Submit" Button
使用R.id。search_go_btn“提交”按钮