首页 > 解决方案 > 滚动到底部时 Android 11 中的键盘闪烁问题

问题描述

我已经编写了代码来在滚动时隐藏键盘。在 Android 11 之下它工作正常。在 Android 11 设备中滚动到底部并单击 edittext 键盘闪烁和消失的问题。

公共类 LinearLayoutThatDetectsSoftKeyboard 扩展 LinearLayout {

public LinearLayoutThatDetectsSoftKeyboard(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public interface Listener {
    public void onSoftKeyboardShown(boolean isShowing);
}
private Listener listener;
public void setListener(Listener listener) {
    this.listener = listener;
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int height = MeasureSpec.getSize(heightMeasureSpec);
    Activity activity = (Activity)getContext();
    Rect rect = new Rect();
    activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
    int statusBarHeight = rect.top;
    int screenHeight = activity.getWindowManager().getDefaultDisplay().getHeight();
    int diff = (screenHeight - statusBarHeight) - height;
    if (listener != null) {
        listener.onSoftKeyboardShown(diff>128); // assume all soft keyboards are at least 128 pixels high
    }
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);       
}

}

上面的代码适用于 Android 11.Android 11 以下的所有设备,而滚动到底部键盘未正确显示。请帮助

标签: androidandroid-softkeyboardandroid-11

解决方案


推荐阅读