隐藏键盘后屏幕跳动。

3

不是在问如何防止键盘弹出时推动屏幕。

当我点击一个EditText时,键盘会显示出来,而屏幕保持不变。但问题是,当我点击EditText之外的区域时,键盘隐藏起来,然后(大约半秒钟后),屏幕会跳动一下(在底部留下一个白色背景),然后再恢复正常。

我正在使用自定义键盘和自定义EditText

在活动中,我已经设置:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

EditText 获得焦点时,我会这样显示键盘:

InputMethodManager inputMethodManager = (InputMethodManager)MainApplication.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput( view, 0 );

EditText失去焦点时隐藏它:

InputMethodManager inputMethodManager = (InputMethodManager)MainApplication.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);

编辑:

请注意,当我在键盘上按下完成时,键盘只是隐藏了(屏幕不会跳动),但EditText并没有失去焦点。因此,如果可以在EditText即将失去焦点时模拟一个完成按键,这对我来说就可以了。

1个回答

1
这是我的解决方法:
我在主布局(一个LinearLayout)上设置了一个OnTouchListener,并在那里隐藏了键盘。
mainLinearLayout.setOnTouchListener( new OnTouchListener() {

    @Override
    public boolean onTouch( View v, MotionEvent event ) {
      InputMethodManager inputMethodManager = (InputMethodManager)MainApplication.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
      inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
      return false; //*SEE NOTE BELOW
    }
} );

*注意:

事实证明,如果我返回true(事件被消耗),键盘会隐藏,但EditText不会失去焦点。如果我返回false(事件尚未被消耗),键盘会隐藏,并且EditText会失去焦点(无论如何,屏幕都不会跳动)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接