1. 程式人生 > >Android監聽軟鍵盤開啟收起事件(軟鍵盤自帶收起按鈕)

Android監聽軟鍵盤開啟收起事件(軟鍵盤自帶收起按鈕)

最近在公司開發cocos2dx上的Android輸入框控制元件,遇到軟鍵盤的事件監聽,通常軟鍵盤的收起方式大致3種:

1.點選軟鍵盤右下角的Return按鈕(系統收起)

2.輸入框焦點時按返回按鈕(系統收起)

3.點選軟鍵盤和輸入框的外部(自發收起)

4.點選軟鍵盤自帶的收起按鈕(軟鍵盤收起)

前三種事件可以監聽,方式都比較簡單

1.點選軟鍵盤右下角的Return按鈕

給輸入框設定監聽

editText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(final TextView v, final int actionId, final KeyEvent event) {
// 可捕捉右下角的Return按鈕

//新增丟擲收起事件程式碼
return false;
}
});

2.輸入框焦點時按返回按鈕

給輸入框增加按鈕監聽

editText.setOnKeyListener(new OnKeyListener() {  
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (listener != null) {
// 可新增丟擲收起事件程式碼
}
return true;
}
return false;
}
});

3.點選軟鍵盤和輸入框的外部

給輸入框的父容器增加觸控監聽

@Override
public boolean onTouchEvent(MotionEvent event) {

if (indexOfChild(editText) > -1) {

// 可新增丟擲收起事件程式碼

}

return super.onTouchEvent(event);

}

4.點選軟鍵盤自帶的收起按鈕(軟鍵盤收起)

問題卡在此處,經過資料的搜查,還是麼能找到軟鍵盤收起按鈕事件監聽的辦法,最後在stackoverflow網站找到了從佈局高度的變化來判斷軟鍵盤的開啟和收起事件。

  1. import android.graphics.Rect;  
  2. import android.view.View;  
  3. import android.view.ViewTreeObserver;  
  4. import java.util.LinkedList;  
  5. import java.util.List;  
  6. publicclass SoftKeyboardStateHelper implements ViewTreeObserver.OnGlobalLayoutListener {  
  7.     publicinterface SoftKeyboardStateListener {  
  8.         void onSoftKeyboardOpened(int keyboardHeightInPx);  
  9.         void onSoftKeyboardClosed();  
  10.     }  
  11.     privatefinal List<SoftKeyboardStateListener> listeners = new LinkedList<SoftKeyboardStateListener>();  
  12.     privatefinal View activityRootView;  
  13.     privateint        lastSoftKeyboardHeightInPx;  
  14.     privateboolean    isSoftKeyboardOpened;  
  15.     public SoftKeyboardStateHelper(View activityRootView) {  
  16.         this(activityRootView, false);  
  17.     }  
  18.     public SoftKeyboardStateHelper(View activityRootView, boolean isSoftKeyboardOpened) {  
  19.         this.activityRootView     = activityRootView;  
  20.         this.isSoftKeyboardOpened = isSoftKeyboardOpened;  
  21.         activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(this);  
  22.     }  
  23.     @Override
  24.     publicvoid onGlobalLayout() {  
  25.         final Rect r = new Rect();  
  26.         //r will be populated with the coordinates of your view that area still visible.
  27.         activityRootView.getWindowVisibleDisplayFrame(r);  
  28.         finalint heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);  
  29.         if (!isSoftKeyboardOpened && heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
  30.             isSoftKeyboardOpened = true;  
  31.             notifyOnSoftKeyboardOpened(heightDiff);  
  32.         } elseif (isSoftKeyboardOpened && heightDiff < 100) {  
  33.             isSoftKeyboardOpened = false;  
  34.             notifyOnSoftKeyboardClosed();  
  35.         }  
  36.     }  
  37.     publicvoid setIsSoftKeyboardOpened(boolean isSoftKeyboardOpened) {  
  38.         this.isSoftKeyboardOpened = isSoftKeyboardOpened;  
  39.     }  
  40.     publicboolean isSoftKeyboardOpened() {  
  41.         return isSoftKeyboardOpened;  
  42.     }  
  43.     /** 
  44.      * Default value is zero (0) 
  45.      * @return last saved keyboard height in px 
  46.      */
  47.     publicint getLastSoftKeyboardHeightInPx() {  
  48.         return lastSoftKeyboardHeightInPx;  
  49.     }  
  50.     publicvoid addSoftKeyboardStateListener(SoftKeyboardStateListener listener) {  
  51.         listeners.add(listener);  
  52.     }  
  53.     publicvoid removeSoftKeyboardStateListener(SoftKeyboardStateListener listener) {  
  54.         listeners.remove(listener);  
  55.     }  
  56.     privatevoid notifyOnSoftKeyboardOpened(int keyboardHeightInPx) {  
  57.         this.lastSoftKeyboardHeightInPx = keyboardHeightInPx;  
  58.         for (SoftKeyboardStateListener listener : listeners) {  
  59.             if (listener != null) {  
  60.                 listener.onSoftKeyboardOpened(keyboardHeightInPx);  
  61.             }  
  62.         }  
  63.     }  
  64.     privatevoid notifyOnSoftKeyboardClosed() {  
  65.         for (SoftKeyboardStateListener listener : listeners) {  
  66.             if (listener != null) {  
  67.                 listener.onSoftKeyboardClosed();  
  68.             }  
  69.         }  
  70.     }  
  71. }  

程式碼的使用:
  1. final SoftKeyboardStateHelper softKeyboardStateHelper = new SoftKeyboardStateHelper(findViewById(R.id.activity_main_layout);  
  2. softKeyboardStateHelper.addSoftKeyboardStateListener(...);  
  3. // then just handle callbacks
SoftKeyboardStateHelper softKeyboardStateHelper = new SoftKeyboardStateHelper(findViewById(R.id.activity_main_layout));
        softKeyboardStateHelper.addSoftKeyboardStateListener(new SoftKeyboardStateHelper.SoftKeyboardStateListener() {
            @Override
            public void onSoftKeyboardOpened(int keyboardHeightInPx) {
            //鍵盤開啟
            }
            @Override
            public void onSoftKeyboardClosed() {
            //鍵盤關閉
            }
        }); 


從中選取了這段程式碼。

這樣能一併解決以上4種情況,比較好的監聽軟鍵盤的開啟和收起事件,目前正在使用中。