標籤:android edittext
最近在公司開發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;public class SoftKeyboardStateHelper implements ViewTreeObserver.OnGlobalLayoutListener { 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(View activityRootView) { this(activityRootView, false); } 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 > 100) { // if more than 100 pixels, its probably a keyboard... isSoftKeyboardOpened = true; notifyOnSoftKeyboardOpened(heightDiff); } else if (isSoftKeyboardOpened && heightDiff < 100) { 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(findViewById(R.id.activity_main_layout);softKeyboardStateHelper.addSoftKeyboardStateListener(...);// then just handle callbacks
地址連結:http://stackoverflow.com/questions/2150078/how-to-check-visibility-of-software-keyboard-in-android
從中選取了這段代碼。
這樣能一併解決以上4種情況,比較好的監聽軟鍵盤的開啟和收合事件,目前正在使用中。
<Android>監聽軟鍵盤開啟收合事件(軟鍵盤內建收合按鈕)