1. 程式人生 > >android 百分百能正確判斷軟鍵盤是否彈出,有個前提條件......

android 百分百能正確判斷軟鍵盤是否彈出,有個前提條件......

1.activity-->android:windowSoftInputMode="adjustResize|stateHidden"(前提條件)

2.如果高版本出現輸入框焦點問題,可由listView改為recycleView

3.監聽註冊監聽,同時需要取消監聽本listener

上程式碼:

import android.app.Activity;
import android.graphics.Rect;
import android.os.Build;
import android.view.View;
import android.view.ViewTreeObserver;

/**
 * 判斷軟鍵盤是否彈出
 * 1.activity-->android:windowSoftInputMode="adjustResize|stateHidden"
 * 2.如果高版本出現輸入框焦點問題,可由listView改為recycleView
 * 3.監聽註冊監聽,同時需要取消監聽本listener
 * Created by Administrator on 2018/3/9.
 */

public class KeyboardUtil {
    public interface OnSoftKeyboardChangeListener {
        void onSoftKeyBoardChange(int softKeyboardHeight, boolean visible);
    }

    /**
     * 監聽軟鍵盤高度和狀態
     * <p>
     * source web link:
     * http://blog.csdn.net/daguaio_o/article/details/47127993
     */
    public static ViewTreeObserver.OnGlobalLayoutListener observeSoftKeyboard(Activity activity, final OnSoftKeyboardChangeListener listener) {
        final View decorView = activity.getWindow().getDecorView();
        ViewTreeObserver.OnGlobalLayoutListener onGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
            int previousKeyboardHeight = -1;
            Rect rect = new Rect();
            boolean lastVisibleState = false;

            @Override
            public void onGlobalLayout() {
                rect.setEmpty();
                decorView.getWindowVisibleDisplayFrame(rect);
                int displayHeight = rect.bottom - rect.top;
                //考慮上狀態列的高度
                int height = decorView.getHeight() - rect.top;
                int keyboardHeight = height - displayHeight;
                if (previousKeyboardHeight != keyboardHeight) {
                    boolean hide = (double) displayHeight / height > 0.8;
                    if (hide != lastVisibleState) {
                        listener.onSoftKeyBoardChange(keyboardHeight, !hide);
                        lastVisibleState = hide;
                    }
                }
                previousKeyboardHeight = height;
            }
        };
        decorView.getViewTreeObserver().addOnGlobalLayoutListener(onGlobalLayoutListener);
        return onGlobalLayoutListener;
    }

    public static void removeSoftKeyboardObserver(Activity activity, ViewTreeObserver.OnGlobalLayoutListener listener) {
        if (listener == null) return;
        final View decorView = activity.getWindow().getDecorView();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            decorView.getViewTreeObserver().removeOnGlobalLayoutListener(listener);
        } else {
            decorView.getViewTreeObserver().removeGlobalOnLayoutListener(listener);
        }
    }
}
import android.os.Bundle;
import android.view.ViewTreeObserver;

import com.co_insight.freshroad.BaseActivity;
import com.co_insight.freshroad.business.utils.KeyboardUtil;

/**
 * 判斷軟鍵盤是否彈出
 * 1.activity-->android:windowSoftInputMode="adjustResize|stateHidden"
 * 2.如果高版本出現輸入框焦點問題,可由listView改為recycleView
 * 3.監聽註冊監聽,同時需要取消監聽本listener
 * Created by Administrator on 2018/3/9.
 */

public class KeyboardActivity extends BaseActivity implements KeyboardUtil.OnSoftKeyboardChangeListener {
    private ViewTreeObserver.OnGlobalLayoutListener mOnGlobalLayoutListener;
    public boolean isKeyboardVisible;// true-->彈出,false-->末彈出,可直接繼承這activity,然後通過這個標誌位判斷軟鍵盤是否彈出.

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mOnGlobalLayoutListener = KeyboardUtil.observeSoftKeyboard(this, this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        KeyboardUtil.removeSoftKeyboardObserver(this, mOnGlobalLayoutListener);
    }

    @Override
    public void onSoftKeyBoardChange(int softKeyboardHeight, boolean visible) {
        isKeyboardVisible = visible;
        System.out.println("判斷軟鍵盤是否彈出"+visible);
    }
}