1. 程式人生 > >監聽鍵盤彈出事件

監聽鍵盤彈出事件

工具類

public class SoftKeyboardStateHelper implements ViewTreeObserver.OnGlobalLayoutListener {
	
    public interface SoftKeyboardStateListener {
        void onSoftKeyboardOpened(int keyboardHeightInPx);
        void onSoftKeyboardClosed();
    }
    
    private static final int KEYBOARD_HEIGHT =
DensityUtil.dip2px(context, 100); private final List<SoftKeyboardStateListener> listeners = new LinkedList<>(); private final View activityRootView; private int lastSoftKeyboardHeightInPx; private boolean isSoftKeyboardOpened; public SoftKeyboardStateHelper(View activityRootView)
{ this(activityRootView, false); } public SoftKeyboardStateHelper(View activityRootView, boolean isSoftKeyboardOpened) { this.activityRootView = activityRootView; this.isSoftKeyboardOpened = isSoftKeyboardOpened; activityRootView.getViewTreeObserver().addOnGlobalLayoutListener
(this); } @Override public void onGlobalLayout() { final Rect r = new Rect(); activityRootView.getWindowVisibleDisplayFrame(r); final int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top); if (!isSoftKeyboardOpened && heightDiff > KEYBOARD_HEIGHT) { // if more than 100 pixels, its probably a keyboard... isSoftKeyboardOpened = true; notifyOnSoftKeyboardOpened(heightDiff); } else if (isSoftKeyboardOpened && heightDiff < KEYBOARD_HEIGHT) { isSoftKeyboardOpened = false; notifyOnSoftKeyboardClosed(); } } public void setIsSoftKeyboardOpened(boolean isSoftKeyboardOpened) { this.isSoftKeyboardOpened = isSoftKeyboardOpened; } public boolean isSoftKeyboardOpened() { return isSoftKeyboardOpened; } public int getLastSoftKeyboardHeightInPx() { return lastSoftKeyboardHeightInPx; } public void addSoftKeyboardStateListener(SoftKeyboardStateListener listener) { listeners.add(listener); } public void removeSoftKeyboardStateListener(SoftKeyboardStateListener listener) { listeners.remove(listener); } private void notifyOnSoftKeyboardOpened(int keyboardHeightInPx) { this.lastSoftKeyboardHeightInPx = keyboardHeightInPx; for (SoftKeyboardStateListener listener : listeners) { if (listener != null) { listener.onSoftKeyboardOpened(keyboardHeightInPx); } } } private void notifyOnSoftKeyboardClosed() { for (SoftKeyboardStateListener listener : listeners) { if (listener != null) { listener.onSoftKeyboardClosed(); } } } }

使用:

	SoftKeyboardStateHelper softKeyboardStateHelper =
			new SoftKeyboardStateHelper(findViewById(R.id.content_wrapper));      //最外層的layout
	        softKeyboardStateHelper.addSoftKeyboardStateListener(
                new SoftKeyboardStateHelper.SoftKeyboardStateListener() {
                    @Override
                    public void onSoftKeyboardOpened(int keyboardHeightInPx) {
                    }
                    @Override
                    public void onSoftKeyboardClosed() {
                    }
                });
    }

windowSoftInputMode = adjustNothing 時該控制元件不會生效
windowSoftInputMode = adjustPan 保證獲得焦點的控制元件不被遮擋,另如果鍵盤彈出前編輯框在位置A ,點選編輯框時彈出鍵盤,且編輯框由動畫上移至位置B,整個佈局還是會向上推移保證位置A不被遮擋。

另華為安手機的安全鍵盤:在使用者名稱和密碼輸入框之間相互切換時會自動切換輸入法,導致開啟鍵盤和關閉鍵盤在不當的時機多次呼叫;