Android中關於鍵盤的處理

強制關閉鍵盤

InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);

開啓軟鍵盤

InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isActive()) {
   imm.showSoftInput(editText, InputMethodManager.RESULT_SHOWN);
   imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
}

一般的關閉軟鍵盤

View view = activity.getWindow().peekDecorView();
if (view != null) {
   InputMethodManager inputmanger = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
   inputmanger.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

監聽軟鍵盤的彈出與關閉

final View rootView = getActivity().getWindow().getDecorView();

        rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Rect rect = new Rect();
                rootView.getWindowVisibleDisplayFrame(rect);
                int rootInvisibleHeight = rootView.getRootView().getHeight() - rect.bottom;
                if(rootInvisibleHeight <= 100){
                    Log.e("doraemon",""軟鍵盤關閉了)
                }else{
                    Log.e("doraemon",""軟鍵盤彈出了)
                }
            }
        });


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章