android 키보드 감청 튕김 및 닫기

5474 단어 andriod
https://blog.csdn.net/Dota_wy/article/details/78781197
전화 키
키 이름 설명 키 값 KEYCODECALL 다이얼 업 5 KEYCODEENDCALL 자동사냥 키 6 KEYCODEHome 키 Home 3 KEYCODEMENU 메뉴 키 82 KEYCODEBACK 반환 키 4 KEYCODESEARCH 검색키 84 KEYCODECAMERA 포토키 27 KEYCODEFOCUS 포토포커스 키 80 KEYCODEPOWER 전원 키 26 KEYCODENOTIFICATION 알림 키 83 KEYCODEMUTE 마이크 무음키 91 KEYCODEVOLUME_MUTE 스피커 음소거 키 164 KEYCODEVOLUME_UP 볼륨 증가 키 24 KEYCODEVOLUME_DOWN 볼륨 감소 키 25
제어 키
키 이름 설명 키 값 KEYCODEENTER Enter Enter 키 66 KEYCODEESCAPE ESC 키 111 KEYCODEDPAD_CENTER 탐색 키 확인 키 23KEYCODEDPAD_UP 탐색 키 위로 19 KEYCODEDPAD_DOWN 탐색 키 아래로 20 KEYCODEDPAD_LEFT 탐색 키를 왼쪽으로 21KEYCODEDPAD_RIGHT 탐색 키를 오른쪽으로 22 KEYCODEMOVE_HOME 커서를 시작 키 122 KEYCODE 로 이동MOVE_END 커서를 끝으로 이동하려면 123 KEYCODEPAGE_UP 위로 페이지 넘기기 92 KEYCODEPAGE_DOWN 아래로 넘기기 93 KEYCODEDEL 체크아웃 키 67 KEYCODEFORWARD_DEL 삭제 키 112 KEYCODEINSERT 삽입 키 124 KEYCODETAB 탭 키 61 KEYCODENUM_LOCK 키패드 잠금 143 KEYCODECAPS_LOCK 대문자 잠금 키 115 KEYCODEBREAK Break/Pause 키 121 KEYCODESCROLL_LOCK 스크롤 잠금 키 116 KEYCODEZOOM_IN 키보드 168 KEYCODEZOOM_OUT 축소 키 169
 
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main5);
    LinearLayout roots = findViewById(R.id.roots);
    EditText editText = findViewById(R.id.editText);
    softKeyboardStateHelper = new SoftKeyboardStateHelper(this);
    softKeyboardStateHelper.addSoftKeyboardStateListener(this);

}

@Override
public void onSoftKeyboardOpened(int keyboardHeightInPx) {
    Toast.makeText(Main5Activity.this, "    ", Toast.LENGTH_SHORT).show();
}

@Override
public void onSoftKeyboardClosed() {
    Toast.makeText(Main5Activity.this, "    ", Toast.LENGTH_SHORT).show();
}

@Override
public void onPointerCaptureChanged(boolean hasCapture) {

}

 
 
 
public class SoftKeyboardStateHelper implements ViewTreeObserver.OnGlobalLayoutListener {
    private View activityRootView;
    private int   lastSoftKeyboardHeightInPx;
    private boolean isSoftKeyboardOpened;
    private int height ;//         

    public interface SoftKeyboardStateListener {
        void onSoftKeyboardOpened(int keyboardHeightInPx);
        void onSoftKeyboardClosed();
    }

    private final List listeners = new LinkedList();


    public SoftKeyboardStateHelper(Activity contextObj) {
        if (contextObj == null) {
            Log.i("dota", "contextObj is null");
            return;
        }
        activityRootView = findContentView(contextObj);
        if (activityRootView!=null){
            activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(this);
        }
//        this(activityRootView, false);
    }

    public SoftKeyboardStateHelper(Activity contextObj, boolean isSoftKeyboardOpened) {
        if (contextObj == null) {
            Log.i("dota", "contextObj is null");
            return;
        }
        activityRootView = findContentView(contextObj);
        if (activityRootView!=null){
            activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(this);
        }
//        this.activityRootView     = activityRootView;
//        this.isSoftKeyboardOpened = isSoftKeyboardOpened;
    }

    private View findContentView(Activity contextObj) {
        return contextObj.findViewById(android.R.id.content);
    }

    @Override
    public void onGlobalLayout() {
        final Rect r = new Rect();
        activityRootView.getWindowVisibleDisplayFrame(r);

        final int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);
        if (heightDiff == 1920){
            height = 200;
        }else {
            height = 100;
        }
        if (!isSoftKeyboardOpened && heightDiff > height ) { // if more than 100 pixels, its probably a keyboard...
            isSoftKeyboardOpened = true;
            notifyOnSoftKeyboardOpened(heightDiff);
        } else if (isSoftKeyboardOpened && heightDiff < height ) {
            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();
            }
        }
    }
}

좋은 웹페이지 즐겨찾기