简体   繁体   English

Android:屏幕键盘出现和消失时,是否有自动调用的监听器?

[英]Android: When the on-screen keyboard appears and disappears, are there any listeners that are automatically called?

I was wondering if there is any way to be notified automatically by android when the on-screen keyboard is shown and when it disappears.我想知道当屏幕键盘显示和消失时,android 是否有任何方法自动通知。

For example when we click on a edittext, the ime appears.例如,当我们单击编辑文本时,会出现 ime。 Will there be any event calls?会有任何事件调用吗? And when it disappears when we press back, similarly will there be any even calls?当我们按下它时它消失了,同样会有任何偶数调用吗?

I found this thread Android: which event fires when on screen keyboard appears?我发现了这个线程Android:屏幕键盘出现时触发哪个事件? , however no answers have been reached yet. ,但是还没有找到答案。

The purpose is because i need an event to automatically manipulate visibility.目的是因为我需要一个事件来自动操纵可见性。 I have an activity with an edittext on the top of the screen, below it, a listview and a linearlayout which are sitting on top of each other.我在屏幕顶部有一个带有edittext的活动,在它下方,一个列表视图和一个线性布局,它们位于彼此之上。 To control what the user sees, i manipulate the visibility.为了控制用户看到的内容,我操纵了可见性。 By default, the linearlayout is shown initially, however, when the user is entering text, the listview should be shown instead.默认情况下,最初显示线性布局,但是,当用户输入文本时,应该显示列表视图。 The listview should disappear when the user has finished typing, which in this case, the on-screen-keyboard will be closed.当用户完成输入时,列表视图应该消失,在这种情况下,屏幕键盘将被关闭。

I tried acomplishing the change of visibility using onFocusChange, however, even when the on-screen keyboard disappears, the edittext still retains focus and the linearlayout never reappears.我尝试使用 onFocusChange 完成可见性的更改,但是,即使屏幕键盘消失,edittext 仍然保持焦点并且线性布局永远不会重新出现。

Below is my implementation of the onFocusChange下面是我对 onFocusChange 的实现

@Override
public void onFocusChange(View v, boolean hasFocus) 
{
    if(v.getId()==R.id.search_screen_keyword_textbox)
    {
        if(hasFocus)
        {
            filterSection.setVisibility(View.GONE);
            autoComSection.setVisibility(View.VISIBLE);
        }
        else
        {
            filterSection.setVisibility(View.VISIBLE);
            autoComSection.setVisibility(View.GONE);
        }
    }       
    else if(v.getId()==R.id.search_screen_location_textbox)
    {
        if(hasFocus)
        {
            filterSection.setVisibility(View.GONE); 
            autoComSection.setVisibility(View.VISIBLE);
        }
        else
        {
            filterSection.setVisibility(View.VISIBLE);
            autoComSection.setVisibility(View.GONE);
        }
    }
    else
    {
        filterSection.setVisibility(View.VISIBLE);
        autoComSection.setVisibility(View.GONE);
    }
}

If anyone has any idea about it do let me know.如果有人对此有任何想法,请告诉我。 :D :D

You can catch the back button when in an edittext, this is what would make the keyboard disappear.您可以在编辑文本中抓住后退按钮,这会使键盘消失。 Using this method:使用此方法:

 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
   if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
           // Do your thing here
           return false;
   }
   return super.dispatchKeyEvent(event);
  }

Search is great: onKeyPreIme or Android API搜索很棒: onKeyPreImeAndroid API

It seems that this thread has a solution using onConfigurationChanged: How to capture the "virtual keyboard show/hide" event in Android?似乎这个线程有一个使用 onConfigurationChanged 的解决方案: How to capture the "virtual keyboard show/hide" event in Android?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM