Android獲取軟鍵盤的高度、鍵盤的打開(kāi)與關(guān)閉、監(jiān)聽(tīng)鍵盤處于打開(kāi)還是關(guān)閉狀態(tài)

最近在項(xiàng)目中,需要獲取到軟鍵盤的高度,再將底部的布局上移鍵盤的高度,話不多說(shuō),直接上代碼:

 //一個(gè)靜態(tài)變量存儲(chǔ)高度
public static int keyboardHeight = 0;
boolean isVisiableForLast = false;
ViewTreeObserver.OnGlobalLayoutListener onGlobalLayoutListener = null;

public void addOnSoftKeyBoardVisibleListener() {
    if (keyboardHeight > 0) {
        return;
    }
    final View decorView = getWindow().getDecorView();
    onGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            Rect rect = new Rect();
            decorView.getWindowVisibleDisplayFrame(rect);
            //計(jì)算出可見(jiàn)屏幕的高度
            int displayHight = rect.bottom - rect.top;
            //獲得屏幕整體的高度
            int hight = decorView.getHeight();
            boolean visible = (double) displayHight / hight < 0.8;
            int statusBarHeight = 0;
            try {
                Class<?> c = Class.forName("com.android.internal.R$dimen");
                Object obj = c.newInstance();
                Field field = c.getField("status_bar_height");
                int x = Integer.parseInt(field.get(obj).toString());
                statusBarHeight = getApplicationContext().getResources().getDimensionPixelSize(x);
            } catch (Exception e) {
                e.printStackTrace();
            }

            if (visible && visible != isVisiableForLast) {
                //獲得鍵盤高度
                keyboardHeight = hight - displayHight - statusBarHeight;
                Log.i("keyboardHeight==1213==", "" + keyboardHeight);

            }
            isVisiableForLast = visible;
        }
    };
    decorView.getViewTreeObserver().addOnGlobalLayoutListener(onGlobalLayoutListener);
}

鍵盤的打開(kāi)與關(guān)閉操作:

public class KeybordS {

/**
 * 打開(kāi)軟鍵盤
 */
public static void openKeybord(EditText mEditText, Context mContext) {
    mEditText.setFocusable(true);
    mEditText.setFocusableInTouchMode(true);
    mEditText.requestFocus();

    InputMethodManager imm = (InputMethodManager) mContext
            .getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.showSoftInput(mEditText, InputMethodManager.RESULT_SHOWN);
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,
            InputMethodManager.HIDE_IMPLICIT_ONLY);
}

/**
 * 關(guān)閉軟鍵盤
 */
public static void closeKeybord(EditText mEditText, Context mContext) {
    InputMethodManager imm = (InputMethodManager) mContext
            .getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);
}

 /**
 * 關(guān)閉軟鍵盤
 */
 public static void hideInput(Activity activity) {
    if (activity.getCurrentFocus() != null) {
        InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(INPUT_METHOD_SERVICE);
        inputManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
    }
}

/**
 * 判斷當(dāng)前軟鍵盤是否打開(kāi)
 */
public static boolean isSoftInputShow(Activity activity) {

    // 虛擬鍵盤隱藏 判斷view是否為空
    View view = activity.getWindow().peekDecorView();
    if (view != null) {
        // 隱藏虛擬鍵盤
        InputMethodManager inputmanger = (InputMethodManager) activity
                .getSystemService(Activity.INPUT_METHOD_SERVICE);
 //  inputmanger.hideSoftInputFromWindow(view.getWindowToken(),0);

        return inputmanger.isActive() && activity.getWindow().getCurrentFocus() != null;
    }
    return false;
}

}

監(jiān)聽(tīng)鍵盤處于打開(kāi)還是關(guān)閉狀態(tài):

     private void setListenerToRootView() {
final View rootView = getWindow().getDecorView().findViewById(android.R.id.content);
rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {

        boolean mKeyboardUp = isKeyboardShown(rootView);
        if (mKeyboardUp) {
            //鍵盤彈出
            //Toast.makeText(getApplicationContext(), "鍵盤彈出", Toast.LENGTH_SHORT).show(); 
        } else {
            //鍵盤收起
            //Toast.makeText(getApplicationContext(), "鍵盤收起", Toast.LENGTH_SHORT).show();
        }
    }
});
}


private boolean isKeyboardShown(View rootView) {
final int softKeyboardHeight = 100;
Rect r = new Rect();
rootView.getWindowVisibleDisplayFrame(r);
DisplayMetrics dm = rootView.getResources().getDisplayMetrics();
int heightDiff = rootView.getBottom() - r.bottom;
return heightDiff > softKeyboardHeight * dm.density;
}
?著作權(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)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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