2016-10-17 6 views
1

내 앱은 데이터를 수집하기위한 것입니다. 그러나 키보드가 보이는 동안에 만 데이터를 수집하면됩니다. 사용자가 입력하는 동안 데이터를 수집하는 것만으로는 충분하지 않으므로 확실히은 키보드가 표시되는지 여부를 알아야합니다. 내가 아는Android에서 소프트 키 보드가 열려 있는지 여부를 감지하는 방법이 있습니까?

는, 유사한 질문은 , (How to check visibility of software keyboard in Android?) 이전에 게시 된하지만 upvotes의 심각한 번호의 마지막 대답은에서, 그리고 나는 많은 것들을 이후 안드로이드 일어난 것 같아요 그때.

그래서 키보드가 열렸는 지/표시되어 있는지 감지 할 수 있습니까?

+0

내가 언급했듯이 마지막 "유용한"대답은 4 년이며 그 이후로 안드로이드로 많은 변화가있었습니다. 이 문제에 대한 최신 답변을 찾고 있는데 – keinabel

답변

2

현재 Android N이 있으며 아직 키보드가 열렸는지 여부를 직접 확인할 방법이 없습니다. 화면 크기를 확인하는 것과 같은 솔루션을 사용할 수 없습니다. 그러나 화면 보호 기능이 없으며 안드로이드 N에서 화면 회전이나 다중 창 모드로 전환 할 때 잘못된 신호를 내기도합니다.

1

들었을 수도 있지만 직접적인 방법은 없습니다. 그러나, 화면 크기가 변경되었는지 여부를 확인하여, 당신은 일반적으로 사용하여이를 찾을 수 있습니다

protected void onSizeChanged(int xNew, int yNew, int xOld, int yOld) { 
    super.onSizeChanged(xNew, yNew, xOld, yOld); 

    if (yOld > yNew) { 
     //Do Stuff Here 
    } 
} 

희망은 내가 도왔 : D

3

에 그런 클래스

package com.dubaipolice.app.utils; 

import android.content.Context; 
import android.content.res.Resources; 
import android.graphics.Rect; 
import android.util.TypedValue; 
import android.view.View; 
import android.view.ViewTreeObserver; 

import java.util.LinkedList; 
import java.util.List; 

/** 
* Created by dev101 on 1/13/15. 
*/ 
public class SoftKeyboardStateHelper implements ViewTreeObserver.OnGlobalLayoutListener { 

    float LIMIT = 100; 

    public interface SoftKeyboardStateListener { 
     void onSoftKeyboardOpened(int keyboardHeightInPx); 

     void onSoftKeyboardClosed(); 
    } 

    private final List<SoftKeyboardStateListener> listeners = new LinkedList<SoftKeyboardStateListener>(); 
    private final View activityRootView; 
    private int lastSoftKeyboardHeightInPx; 
    private boolean isSoftKeyboardOpened; 

    public SoftKeyboardStateHelper(Context context, View activityRootView) { 
     this(activityRootView, false); 
     Resources r = context.getResources(); 
     LIMIT = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, r.getDisplayMetrics()); 
    } 

    public SoftKeyboardStateHelper(View activityRootView, boolean isSoftKeyboardOpened) { 
     this.activityRootView = activityRootView; 
     this.isSoftKeyboardOpened = isSoftKeyboardOpened; 
     activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(this); 
    } 

    @Override 
    public void onGlobalLayout() { 
     final Rect r = new Rect(); 
     //r will be populated with the coordinates of your view that area still visible. 
     activityRootView.getWindowVisibleDisplayFrame(r); 

     final int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top); 
     if (!isSoftKeyboardOpened && heightDiff > LIMIT) { // if more than 100 pixels, its probably a keyboard... 
      isSoftKeyboardOpened = true; 
      notifyOnSoftKeyboardOpened(heightDiff); 
     } else if (isSoftKeyboardOpened && heightDiff < LIMIT) { 
      isSoftKeyboardOpened = false; 
      notifyOnSoftKeyboardClosed(); 
     } 
    } 

    public void setIsSoftKeyboardOpened(boolean isSoftKeyboardOpened) { 
     this.isSoftKeyboardOpened = isSoftKeyboardOpened; 
    } 

    public boolean isSoftKeyboardOpened() { 
     return isSoftKeyboardOpened; 
    } 

    /** 
    * Default value is zero (0) 
    * 
    * @return last saved keyboard height in px 
    */ 
    public int getLastSoftKeyboardHeightInPx() { 
     return lastSoftKeyboardHeightInPx; 
    } 

    public void addSoftKeyboardStateListener(SoftKeyboardStateListener listener) { 
     listeners.add(listener); 
    } 

    public void removeSoftKeyboardStateListener(SoftKeyboardStateListener listener) { 
     listeners.remove(listener); 
    } 

    private void notifyOnSoftKeyboardOpened(int keyboardHeightInPx) { 
     this.lastSoftKeyboardHeightInPx = keyboardHeightInPx; 

     for (SoftKeyboardStateListener listener : listeners) { 
      if (listener != null) { 
       listener.onSoftKeyboardOpened(keyboardHeightInPx); 
      } 
     } 
    } 

    private void notifyOnSoftKeyboardClosed() { 
     for (SoftKeyboardStateListener listener : listeners) { 
      if (listener != null) { 
       listener.onSoftKeyboardClosed(); 
      } 
     } 
    } 
} 

를 작성하여 활동의 생성, 다음 줄 추가

final SoftKeyboardStateHelper softKeyboardStateHelper = new SoftKeyboardStateHelper(context, findViewById(R.id.parent)); 
     softKeyboardStateHelper.addSoftKeyboardStateListener(softKeyboardStateListener); 

여기서 R.id.parent는 활동의 ID입니다. 상위 레이아웃 및 softKeyboardStateListener는 다음과 같이 정의됩니다.

+0

이 저에게 적합합니다! 고마워요 :) – error1337

+0

이것은 작동합니다. 감사! –

0

이전에 똑같은 문제에 직면했습니다. 많은 시행 착오 끝에 다른 사람들의 제안을 따랐을 때 저에게 잘 맞는 구현 방법을 찾았습니다. link입니다.

관련 문제