繁体   English   中英

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

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

我想知道当屏幕键盘显示和消失时,android 是否有任何方法自动通知。

例如,当我们单击编辑文本时,会出现 ime。 会有任何事件调用吗? 当我们按下它时它消失了,同样会有任何偶数调用吗?

我发现了这个线程Android:屏幕键盘出现时触发哪个事件? ,但是还没有找到答案。

目的是因为我需要一个事件来自动操纵可见性。 我在屏幕顶部有一个带有edittext的活动,在它下方,一个列表视图和一个线性布局,它们位于彼此之上。 为了控制用户看到的内容,我操纵了可见性。 默认情况下,最初显示线性布局,但是,当用户输入文本时,应该显示列表视图。 当用户完成输入时,列表视图应该消失,在这种情况下,屏幕键盘将被关闭。

我尝试使用 onFocusChange 完成可见性的更改,但是,即使屏幕键盘消失,edittext 仍然保持焦点并且线性布局永远不会重新出现。

下面是我对 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);
    }
}

如果有人对此有任何想法,请告诉我。 :D

您可以在编辑文本中抓住后退按钮,这会使键盘消失。 使用此方法:

 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);
  }

搜索很棒: onKeyPreImeAndroid API

似乎这个线程有一个使用 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