6

我想知道当屏幕键盘显示和消失时是否有任何方法可以由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

4

2 回答 2

2

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

 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

于 2011-07-04T14:58:45.317 回答
1

It seems that this thread has a solution using onConfigurationChanged: How to capture the "virtual keyboard show/hide" event in Android?

于 2011-07-04T14:39:59.827 回答