简体   繁体   English

Android:按下搜索按钮时显示白色背景

[英]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.OnCloseListenerSearchManager.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.

相关问题 当按下(和)主页按钮(背景)时如何显示通知 - How to show nottification when (andorid) home button is pressed (background) 为什么在Android中按下背景按钮时没有焦点? - Why Button with Background gets no focus when pressed in android? 当按下按钮时更改android中活动背景上的图像 - when button pressed change images on background on activity in android 按下时更改按钮背景 - Change button background when pressed Android-如何在按下键盘的搜索按钮时隐藏键盘? - Android - How to hide keyboard when search button of the keyboard is pressed? 按下搜索按钮时如何避免 DialogFragment 被关闭 - Android - How to avoid a DialogFragment being dismissed when the Search button is pressed - Android 按下后退按钮时如何显示非页内广告 - How to Show Interstitial ads when back Button Pressed Android 按下Android后退按钮时如何显示确认警报? - How to show confirm alert when Android Back button is pressed? 在Android中按下后退按钮时显示所有片段 - All fragments show when back button is pressed in Android 当应用程序从后台返回时而不是按下后退按钮时如何显示dilaog - How to show a dilaog when app returns from background and not when back button is pressed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM