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

问题描述 投票:1回答:1

根据我的需要,我已经在屏幕上实现了可移动的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;

}
android
1个回答
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
    }
});
© www.soinside.com 2019 - 2024. All rights reserved.