Android鍵盤監(jiān)聽工具類

  • 將狀態(tài)欄透明后(沉浸式),導(dǎo)致鍵盤模式失效,無法自動(dòng)將輸入框上移,所以需要監(jiān)聽頁(yè)面的高度變化的差值,從而計(jì)算出鍵盤高度,手動(dòng)將底部的輸入框上移
import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.inputmethod.InputMethodManager;

import androidx.annotation.NonNull;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleEventObserver;
import androidx.lifecycle.LifecycleOwner;

/// 鍵盤監(jiān)聽工具類
public final class KeyboardUtil {
    public static void hideKeyboard(Activity activity) {
        InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (inputMethodManager != null) {
            inputMethodManager.hideSoftInputFromWindow(activity.getWindow().getDecorView().getWindowToken(), 0);
        }
    }

    // 注冊(cè)鍵盤監(jiān)聽
    public static void registerKeyboardStateCallback(
            @NonNull LifecycleOwner lifecycleOwner,
            @NonNull Activity activity,
            @NonNull KeyboardStateCallback callback
    ) {
        new KeyboardStateWatcher(lifecycleOwner, activity, callback);
    }

    public interface KeyboardStateCallback {
        void onShow(int keyboardHeight);

        void onHide();
    }

    private static class KeyboardStateWatcher implements LifecycleEventObserver, ViewTreeObserver.OnGlobalLayoutListener {

        private final Rect windowVisibleRect = new Rect();

        private final Lifecycle lifecycle;

        private View rootView;

        private KeyboardStateCallback callback;

        private int rootViewVisibleHeight = 0;

        public KeyboardStateWatcher(@NonNull LifecycleOwner lifecycleOwner, @NonNull Activity activity, @NonNull KeyboardStateCallback callback) {
            this.lifecycle = lifecycleOwner.getLifecycle();
            this.lifecycle.addObserver(this);

            this.rootView = activity.getWindow().getDecorView();
            this.rootView.getViewTreeObserver().addOnGlobalLayoutListener(this);

            this.callback = callback;
        }

        private void analyzeKeyboardState(View rootView) {
            rootView.getWindowVisibleDisplayFrame(windowVisibleRect);
            int visibleHeight = windowVisibleRect.height();
            if (rootViewVisibleHeight == 0) {
                rootViewVisibleHeight = visibleHeight;
                return;
            }
            if (rootViewVisibleHeight == visibleHeight) {
                return;
            }
            if (rootViewVisibleHeight - visibleHeight > 200) {
                callback.onShow(rootViewVisibleHeight - visibleHeight);
                rootViewVisibleHeight = visibleHeight;
            } else if (visibleHeight - rootViewVisibleHeight > 200) {
                callback.onHide();
                rootViewVisibleHeight = visibleHeight;
            }
        }

        @Override
        public void onStateChanged(@NonNull LifecycleOwner source, @NonNull Lifecycle.Event event) {
            if (event == Lifecycle.Event.ON_DESTROY) {
                rootView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                lifecycle.removeObserver(this);

                rootView = null;
                callback = null;
            }
        }

        @Override
        public void onGlobalLayout() {
            analyzeKeyboardState(rootView);
        }
    }
}
  • 簡(jiǎn)單使用
    KeyboardUtil.registerKeyboardStateCallback(this, this, new KeyboardUtil.KeyboardStateCallback() {
    @Override
    public void onShow(int keyboardHeight) {
        // 鍵盤彈起,讓底部輸入框向上移動(dòng)
        binding.editText.setTranslationY(-keyboardHeight);
    }

    @Override
    public void onHide() {
        // 鍵盤隱藏
    }
});
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容