2016-06-20 89 views
2

转向我有一个全屏景观应用(的Cocos2D-X/NDK)与我在运行时这样设置Android的,怎么知道有多少我的看法是通过软键盘adjustPan

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mInstance.mEditText.getLayoutParams(); 
params.topMargin = (int)newY; 
params.addRule(RelativeLayout.ALIGN_PARENT_TOP); 
mInstance.mEditText.setLayoutParams(params); 
哪个位置输入字段

它在大多数设备上都很酷,但我有一个设备,其中软键盘动态增长,其上方有'建议'行。 那是奇怪的事情发生。

如果我的EditText位于屏幕顶部某处,即它的矩形不与键盘相交,则没有任何变化,EditText保持在原来的位置,一切正常。 但是,如果我的EditText位于屏幕的中间,所以键盘将与其重叠,它将开始向上移动(使用动画)。我成功地利用SOFT_INPUT_ADJUST_PAN确定键盘高度(通过差getWindowVisibleDisplayFrame调用之间),所以我不能只是禁止设置adjustNothing标志平移

,我要坚持adjustPan

问题是,我无法理解软键盘调整大小时对于我的视图位置究竟做了什么(对于建议行)。 在我onGlobalLayout我试着看:

- TextView getY(), 
- TextView getTranslationY(), 
- TextView RelativeLayout params (topMargin, bottomMargin), 
- TextView getLocationOnScreen, 
- TextView getGlobalVisibleRect top & bottom, 
- Activity getWindowVisibleDisplayFrame rect top & bottom. 

没有上述改变!但TextView无论如何都在向上移动。 我相信有一些偏移量在视图树中应用了SOMEWHERE,出现软键盘之后。但为什么getLocationOnScreen不显示这个?为什么getGlobalVisibleRect对位置变化不了解?

我想设置精确的位置对我的EditText,所以即使我不能阻止它动起来(有adjustPan标志设置的权衡),至少我需要知道多少上升,申请转换

我尝试了很哈克解决方法:在我的EditText beforeTextChanged()方法我设置的TextView的布局TOPMARGIN为0,以确保建议线出现时,它不会因为它不再重叠改变我的TextView。然后在onGlobalLayout()我恢复正确的EditText位置。它有效,但我不喜欢这种方式,也导致我的文字闪烁。

一些代码,用于更好地理解我在做什么。

private void showKeyboard(final boolean show) { 
    final InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE); 
    if (show) { 
     mEditText.requestFocus(); 
     new Handler().postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       imm.showSoftInput(mEditText, 0); 
      } 
     }, 50); 
     mVisible = true; 
    } else { 
     imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0); 
     mEditText.clearFocus(); 
     mVisible = false; 
    } 
} 

@Override 
protected void onCreate(final Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.native_keyboard); 
    mLayout = findViewById(R.id.keyboard_layout); 
    mEditText = (EditText) findViewById(R.id.keyboard_input); 
    mEditText.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI); 
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN); 
    mEditText.setInputType(mEditText.getInputType() | EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE | EditorInfo.TYPE_TEXT_FLAG_NO_SUGGESTIONS | EditorInfo.TYPE_TEXT_FLAG_AUTO_COMPLETE); 
    mInitialFrame = getVisibleFrame(); 
    mPreviousFrame = mInitialFrame; 
    mLayout.getViewTreeObserver().addOnGlobalLayoutListener(new android.view.ViewTreeObserver.OnGlobalLayoutListener() { 
     @Override 
     public void onGlobalLayout() { 
      final int kbAssumedH = 100; 
      Rect currentFrame = getVisibleFrame(); 
      final int dResize = (int) (mPreviousFrame.height() - currentFrame.height()); 
      if (mWasShown && isVisible() && dResize != 0) { 
       NativeKeyboard.onKeyboardResized(); 
      } 
      mPreviousFrame = currentFrame; 

      if (isVisible() && (mInitialFrame.height() - currentFrame.height() > kbAssumedH)) { 
       if (!mWasShown) { 
        NativeKeyboard.onKeyboardDidShow(); 
        mWasShown = true; 
       } 
      } else { 
       if (mWasShown) { 
        NativeKeyboard.onKeyboardDidHide(); 
        mWasShown = false; 
       } 
      } 
     } 
    });  
} 

这就是我得到的是不与键盘重叠当前窗口的空间:

public static synchronized android.graphics.Rect getVisibleFrame() { 
    View rootView = mInstance.getWindow().getDecorView(); 
    android.graphics.Rect r = new android.graphics.Rect(); 
    rootView.getWindowVisibleDisplayFrame(r); 
    return r; 
} 

这是我使用的设置我的EditText位置的方法。 在建议行出现后,我的EditText在视觉上相对于我作为Y参数传递的位置向上移动(并且该移位是永久的,即我可以调用用的setRect新的位置,并且它也将被转移)

public static synchronized void setRect(final float x, final float y, final float w, final float h) { 
    if (mInstance != null) { 
     Cocos2dxHelper.runOnUIThread(new Runnable() { 
      @Override 
      public void run() { 
        mInstance.mEditText.setX((int) x); 
        float height = mInstance.mEditText.getHeight(); 
        mInstance.mEditText.setWidth((int) w); 
        float yCenter = y + h/2; 
        float newY = yCenter - height/2.f; 
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mInstance.mEditText.getLayoutParams(); 
        params.topMargin = (int)newY; 
        params.width = (int)w; 
        params.addRule(RelativeLayout.ALIGN_PARENT_TOP); 
        mInstance.mEditText.setLayoutParams(params); 
      } 
     }); 
    } 
} 

我试图在4个不同的设备(3个手机& 1片),仅在宏基液体玉Z,4.4.4发生问题。

在此先感谢。

回答

0

那么,我没有设法赶上过渡本身。 相反,那是在我的具体情况确定解决办法是这样的:

添加新的不可见的对话框,用它getWindowVisibleDisplayFrame,所以你会得到所有你需要时,会显示/隐藏键盘。

...并且当然您现在需要SOFT_INPUT_ADJUST_NOTHING作为主(可见)对话框,为什么不,如果您不再需要它的displayFrame?所以,没有隐藏的不明显的变化适用于您的观点。

那很简单。 希望它有帮助。

相关问题