[英]Android: show white background when search button pressed
When I press the search key on my device, I want it to show a white background. 当我按设备上的搜索键时,我希望它显示白色背景。 However, when I press the back button, I want the previous activity's background to be restored. 但是,当我按下“后退”按钮时,我希望恢复上一个活动的背景。 ivBackground
is a variable I added to my relativelayout which I turn VISIBLE to show the white background. ivBackground
是我添加到我的相对ivBackground
中的变量,我将其变为可见以显示白色背景。
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_SEARCH) {
ivBackground.setVisibility(View.VISIBLE);//WHITE IMAGEVIEW
return false;
} else if (keyCode == KeyEvent.KEYCODE_BACK) {
ivBackground.setVisibility(View.GONE);
return true;
}
return super.onKeyDown(keyCode, event);
}
While the above code works, the problem is that when I press the back button, the white screen still remains. 虽然上面的代码有效,但问题是当我按下“后退”按钮时,白屏仍然存在。 It only goes away if I press the back button once again. 只有再次按下返回按钮,它才会消失。 Any solutions? 有什么办法吗?
On the relevant activity, you can get a reference to a SearchManager object. 在相关活动上,您可以获取对SearchManager对象的引用。 On this, you can set an OnDismissListener
, which is called the when search UI is dismissed eg 在此,您可以设置一个OnDismissListener
,它被称为当关闭搜索UI时,例如
this.searchMgr = (SearchManager)this.getSystemService(Context.SEARCH_SERVICE);
this.searchMgr.setOnDismissListener(new OnDismissListener() {
public void onDismiss() {
ivBackground.setVisibility(View.GONE);
}
});
To make the white background visible, you can override onSearchRequested
inside your activity class, which is called when a user signals the desire to start a search 为了使白色背景可见,您可以覆盖活动类中的onSearchRequested
,当用户发出希望开始搜索的信号时调用
@Override
public boolean onSearchRequested() {
ivBackground.setVisibility(View.VISIBLE);
return super.onSearchRequested();
}
Hope this helps! 希望这可以帮助!
How about setting a SearchView.OnCloseListener or a SearchManager.OnDismissListener that also hides your view? 如何设置同时隐藏您的视图的SearchView.OnCloseListener或SearchManager.OnDismissListener ? This seems like the right solution anyway since the two events are logically connected. 无论如何,这似乎是正确的解决方案,因为两个事件在逻辑上是联系在一起的。 If you later dismiss the search view programmatically for example, you don't have to worry about separately dismissing the background view. 例如,如果您以后以编程方式关闭搜索视图,则不必担心分别关闭背景视图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.