수색…


사용자가 화면의 다른 곳을 탭할 때 키보드 숨기기

활동에 코드를 추가하십시오.

이것은 또한 조각에 대한 조각에이 코드를 추가 할 필요가 작동하지 않을 것입니다.

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    View view = getCurrentFocus();
    if (view != null && (ev.getAction() == MotionEvent.ACTION_UP || ev.getAction() == MotionEvent.ACTION_MOVE) && view instanceof EditText && !view.getClass().getName().startsWith("android.webkit.")) {
        int scrcoords[] = new int[2];
        view.getLocationOnScreen(scrcoords);
        float x = ev.getRawX() + view.getLeft() - scrcoords[0];
        float y = ev.getRawY() + view.getTop() - scrcoords[1];
        if (x < view.getLeft() || x > view.getRight() || y < view.getTop() || y > view.getBottom())
        ((InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow((this.getWindow().getDecorView().getApplicationWindowToken()), 0);
    }
    return super.dispatchTouchEvent(ev);
}

키보드 열기 및 닫기 콜백 등록

아이디어는 각 변경 전후의 레이아웃을 측정하고 중요한 변경 사항이있는 경우 해당 소프트 키 보드가 어느 정도 확실 할 수 있습니다.

// A variable to hold the last content layout hight
private int mLastContentHeight = 0;

private ViewTreeObserver.OnGlobalLayoutListener keyboardLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override public void onGlobalLayout() {
        int currentContentHeight = findViewById(Window.ID_ANDROID_CONTENT).getHeight();

        if (mLastContentHeight > currentContentHeight + 100) {
            Timber.d("onGlobalLayout: Keyboard is open");
            mLastContentHeight = currentContentHeight;
        } else if (currentContentHeight > mLastContentHeight + 100) {
            Timber.d("onGlobalLayout: Keyboard is closed");
            mLastContentHeight = currentContentHeight;
        }
    }
};

그런 다음 onCreate 에서 mLastContentHeight 의 초기 값을 설정합니다.

mLastContentHeight = findViewById(Window.ID_ANDROID_CONTENT).getHeight();

청취자를 추가합니다.

rootView.getViewTreeObserver().addOnGlobalLayoutListener(keyboardLayoutListener);

destroy 리스너를 제거하는 것을 잊지 마세요.

rootView.getViewTreeObserver().removeOnGlobalLayoutListener(keyboardLayoutListener);


Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow