【问题标题】:Screen jumps after hiding keyboard隐藏键盘后屏幕跳转
【发布时间】:2015-07-29 16:39:56
【问题描述】:

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

当我单击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 即将失去焦点时模拟 DONE 按下,这对我来说没问题。

【问题讨论】:

    标签: android keyboard


    【解决方案1】:

    我就是这样解决的:

    我在主布局上设置了一个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 失去焦点(无论哪种方式,都不会跳屏)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-28
      • 2015-04-13
      • 1970-01-01
      • 2015-01-04
      • 2021-06-23
      • 2014-04-24
      • 1970-01-01
      相关资源
      最近更新 更多