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網站找到了從佈局高度的變化來判斷軟鍵盤的開啟和收起事件。
-
import android.graphics.Rect;
- import android.view.View;
- import android.view.ViewTreeObserver;
- import java.util.LinkedList;
- import java.util.List;
- publicclass SoftKeyboardStateHelper implements ViewTreeObserver.OnGlobalLayoutListener {
- publicinterface SoftKeyboardStateListener {
- void onSoftKeyboardOpened(int keyboardHeightInPx);
- void onSoftKeyboardClosed();
- }
- privatefinal List<SoftKeyboardStateListener> listeners = new LinkedList<SoftKeyboardStateListener>();
- privatefinal View activityRootView;
- privateint lastSoftKeyboardHeightInPx;
- privateboolean isSoftKeyboardOpened;
- public SoftKeyboardStateHelper(View activityRootView) {
- this(activityRootView, false);
- }
- public SoftKeyboardStateHelper(View activityRootView, boolean isSoftKeyboardOpened) {
- this.activityRootView = activityRootView;
- this.isSoftKeyboardOpened = isSoftKeyboardOpened;
- activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(this);
- }
- @Override
- publicvoid onGlobalLayout() {
- final Rect r = new Rect();
- //r will be populated with the coordinates of your view that area still visible.
- activityRootView.getWindowVisibleDisplayFrame(r);
- finalint heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);
- if (!isSoftKeyboardOpened && heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
- isSoftKeyboardOpened = true;
- notifyOnSoftKeyboardOpened(heightDiff);
- } elseif (isSoftKeyboardOpened && heightDiff < 100) {
- isSoftKeyboardOpened = false;
- notifyOnSoftKeyboardClosed();
- }
- }
- publicvoid setIsSoftKeyboardOpened(boolean isSoftKeyboardOpened) {
- this.isSoftKeyboardOpened = isSoftKeyboardOpened;
- }
- publicboolean isSoftKeyboardOpened() {
- return isSoftKeyboardOpened;
- }
- /**
- * Default value is zero (0)
- * @return last saved keyboard height in px
- */
- publicint getLastSoftKeyboardHeightInPx() {
- return lastSoftKeyboardHeightInPx;
- }
- publicvoid addSoftKeyboardStateListener(SoftKeyboardStateListener listener) {
- listeners.add(listener);
- }
- publicvoid removeSoftKeyboardStateListener(SoftKeyboardStateListener listener) {
- listeners.remove(listener);
- }
- privatevoid notifyOnSoftKeyboardOpened(int keyboardHeightInPx) {
- this.lastSoftKeyboardHeightInPx = keyboardHeightInPx;
- for (SoftKeyboardStateListener listener : listeners) {
- if (listener != null) {
- listener.onSoftKeyboardOpened(keyboardHeightInPx);
- }
- }
- }
- privatevoid notifyOnSoftKeyboardClosed() {
- for (SoftKeyboardStateListener listener : listeners) {
- if (listener != null) {
- listener.onSoftKeyboardClosed();
- }
- }
- }
- }
程式碼的使用:
- final SoftKeyboardStateHelper softKeyboardStateHelper = new SoftKeyboardStateHelper(findViewById(R.id.activity_main_layout);
- softKeyboardStateHelper.addSoftKeyboardStateListener(...);
- // then just handle callbacks
softKeyboardStateHelper.addSoftKeyboardStateListener(new SoftKeyboardStateHelper.SoftKeyboardStateListener() {
@Override
public void onSoftKeyboardOpened(int keyboardHeightInPx) { //鍵盤開啟
}
@Override
public void onSoftKeyboardClosed() { //鍵盤關閉
}
});
從中選取了這段程式碼。
這樣能一併解決以上4種情況,比較好的監聽軟鍵盤的開啟和收起事件,目前正在使用中。