문제

내 앱에 ActionBar 탭을 구현했습니다.하지만 탭 변경 중에 한 가지 문제에 직면했습니다.내 탭에는 주로 webview가 포함되어 있지만 한 탭에는 편집 텍스트가 포함되어 있습니다. 텍스트 편집을 클릭하면 키보드가 나타나고, 탭을 변경하면 키보드가 나타나는데 키보드가 사라지지 않습니다..명시적으로 숨기는 등의 간단한 솔루션을 몇 가지 시도했지만 성공하지 못했습니다.

InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
 mgr.hideSoftInputFromWindow(fragment.getView().getApplicationWindowToken(), 0);

이것은 ActionBar.TabListener를 구현하는 클래스의 onTabSelected()에서 호출하고 있습니다.이 문제를 해결하는 방법도 모르고 관련 정보도 얻지 못합니다.

미리 감사드립니다.어떤 도움이라도 감사하겠습니다.

업데이트 및 답변

Eric의 답변은 나에게 어느 정도 힘을 주었고 답변을 얻는 데 도움이 되었기 때문에 변경 사항에 따라 그의 답변이 올바른 것으로 표시됩니다.즉, 내 코드에 eric의 코드를 추가했습니다. onTabUnselected 하지만 tabSelected에는 없습니다. 그 순간 뷰를 얻으려고 할 때 뷰가 생성되지 않아 뷰가 null로 표시되었습니다.그래서 내 최종 코드는

@Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft)
    {
        View target = initialisedFragment.getView().findFocus();

        if (target != null) 
        {
            InputMethodManager mgr = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
            mgr.hideSoftInputFromWindow(target.getWindowToken(), 0);
        }
    }
도움이 되었습니까?

해결책

난 당신이 단지 하나를 선택할 수 있다고 믿지 않아요 View 이를 창 토큰으로 사용합니다.현재 키보드가 표시되는 필드를 찾아야 합니다.

이것은 이전에 사용한 방법의 포트입니다. 시도해 볼 가치가 있습니다.

View target = fragment.getView().findFocus();
if (target != null) {
    InputMethodManager imm = (InputMethodManager) target.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(target.getWindowToken(), 0);
}

그래도 작동하지 않으면 다음이 있습니다. 작동하는 것으로 보고된 다른 많은 방법.

다른 팁

i 다음을 사용합니다.이 옵션은 장치가 작동하는 현재보기를 캡처합니다.

public final void onTabReselected(Tab tab, FragmentTransaction fragmentTransaction) {
        View focus = getCurrentFocus();
        if (focus != null) {
            hiddenKeyboard(focus);
        }
    }
public final void onTabselected(Tab tab, FragmentTransaction fragmentTransaction) {
        View focus = getCurrentFocus();
        if (focus != null) {
            hiddenKeyboard(focus);
        }
    }
public final void onTabUnselected(Tab tab, FragmentTransaction fragmentTransaction) {
        View focus = getCurrentFocus();
        if (focus != null) {
            hiddenKeyboard(focus);
        }
    }
.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top