隐藏键盘后屏幕跳转

时间:2015-07-29 06:57:50

标签: android keyboard

询问如何防止键盘在弹出时向上推屏幕。

当我点击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);

修改

当我在键盘上按 DONE 时,键盘只是隐藏(没有屏幕跳转),但EditText没有失去焦点。因此,当EditText即将失去焦点时,如果可以模拟完成,那么这对我来说没问题。

1 个答案:

答案 0 :(得分:0)

这就是我解决它的方法:

我在主要版面上设置OnTouchListener(它是LinearLayout)并将键盘隐藏在那里:

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会失去焦点(无论哪种方式,都没有屏幕跳转)

相关问题