Android中打开键盘时可移动的EditText隐藏问题

时间:2017-12-19 13:24:44

标签: android

根据我的需要,我已经在屏幕上实现了可移动的EditText,但问题是当EditText移动到底部并打开键盘时,EditText会隐藏并且不会显示。我该怎么解决这个问题。    editText.setOnTouchListener(ImageStickerActivity.this);

 float newDx;
float newDy;
int latAPerformedAction;
@Override
public boolean onTouch(View view, MotionEvent event) {
    editText.setCursorVisible(true);
    switch (event.getActionMasked()) {
        case MotionEvent.ACTION_DOWN:
            newDy = view.getY() - event.getRawY();
            latAPerformedAction = MotionEvent.ACTION_DOWN;
            isMoving = false;
            break;

        case MotionEvent.ACTION_MOVE:
            float newY = event.getRawY() + newDy;

            if (newY <= 0 || newY >= screenHight - view.getHeight()) {
                latAPerformedAction = MotionEvent.ACTION_MOVE;

                break;
            }
            isMoving = true;
            view.setY(newY);
            latAPerformedAction = MotionEvent.ACTION_MOVE;
            break;

        case MotionEvent.ACTION_UP:
            if (latAPerformedAction == MotionEvent.ACTION_DOWN)
                isMoving = false;
            break;

        default:
            isMoving = false;
            return false;
    }
    return false;

}

1 个答案:

答案 0 :(得分:0)

我不知道这是否适用于您的自定义EditText实现,但您可以尝试在AndroidManifest中设置以下属性

<activity android:windowSoftInputMode="adjustResize"> ... </activity>

如果这不起作用,您可以选择添加事件侦听器以查看视口何时更改。这是在键盘显示或隐藏时检索事件的常用方法。然后,您可以向上或向下移动EditText像视口更改一样多的像素。这样,EditText始终位于键盘上方。

以下是您可以为视口侦听器尝试的示例代码:

rootLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    public void onGlobalLayout(){
        int viewportHeightDiff = rootLayout.getRootView().getHeight() - rootLayout.getHeight();
        // TODO: Move your editText by viewportHeightDiff, this is how much difference 
        // there is between the viewport height and the screen height
    }
});