我有一个包含searchview
的操作栏。当用户单击搜索按钮并折叠搜索视图时,操作栏会在左侧显示后退按钮。
我们如何检测用户何时点击此后退按钮?
修改
根据答案我检查了OnOptionsItemSelected
,但它没有打电话。这是我OnOptionsItemSelected
的代码:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (item != null && id == android.R.id.home) {
if (mNavigationDrawerFragment.isDrawerOpen(Gravity.RIGHT)) {
mNavigationDrawerFragment.closeDrawer(Gravity.RIGHT);
} else {
mNavigationDrawerFragment.openDrawer(Gravity.RIGHT);
}
return true;
}
if (id == R.id.action_search) {
return true;
}
return super.onOptionsItemSelected(item);
}
答案 0 :(得分:12)
将它放在onCreateOptionsMenu方法上:
MenuItemCompat.setOnActionExpandListener(menu.findItem(R.id.action_search), new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
//DO SOMETHING WHEN THE SEARCHVIEW IS CLOSING
return true;
}
});
答案 1 :(得分:5)
您应该为manifest.xml添加元数据,以用于您想要的活动
喜欢
<activity
android:name=".Example"
android:label="@string/Example"
android:theme="Theme.AppCompat.Light">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
并且您的代码在示例中应该是这样的
@Override
protected void onCreate(Bundle savedInstanceState) {
.......
getActionBar().setDisplayHomeAsUpEnabled(true);
......
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
答案 2 :(得分:2)
只需覆盖以下方法即可。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
//do whatever you want to do here.
}
return true;
}
答案 3 :(得分:0)
尝试此操作:使用OnQueryTextListener而不是使用onOptionsItemSelected来检测搜索更改。然后使用后退按钮(以及查询文本更改的任何其他时间)调用onQueryTextChanged。
设置菜单时,将searchView分配给监听器。每当搜索条件发生变化时(在除搜索按钮之外的任何键盘按下)都会调用onQueryTextChange,并且当按下后退按钮时,将使用空白字符串调用它。当在键盘上按下搜索按钮时,将调用onQueryTextSubmit。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
final SearchView searchView = (SearchView)menu.findItem(R.id.action_search).getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
Log.i(TAG,"onQueryTextSubmit: " + s);
searchView.clearFocus();
return true;
}
@Override
public boolean onQueryTextChange(String s) {
Log.i(TAG,"onQueryTextChange: " + s);
FragmentManager fragmentManager = getSupportFragmentManager();
Fragment fragment = fragmentManager.findFragmentByTag(currentFragmentTag);
if (fragment.getClass().getName().startsWith("com.mydomain.myapp.mainactivity.MyFragment")) {
if (s.isEmpty()) {
((SingleICPListFragment)fragment).clearSearchCriteria();
} else {
((SingleICPListFragment) fragment).applySearchCriteria(s);
}
}
return true;
}
});
SearchManager searchManager = (SearchManager)getSystemService(Context.SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return true;
}
答案 4 :(得分:0)
可能的doio.c副本
无论如何:
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Do what you want to do
}
});