Android监听软键盘打开收起事件(软键盘自带收起按钮)

时间:2023-02-20 09:35:10

最近在公司开发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.点击软键盘自带的收起按钮(软键盘收起)

问题卡在此处,经过资料的搜查,还是么能找到软键盘收起按钮事件监听的办法,最后在*网站找到了从布局高度的变化来判断软键盘的打开和收起事件。

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

代码的使用:

[java] view plain copy
  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() {
            //键盘关闭
            }
        }); 


地址链接:http://*.com/questions/2150078/how-to-check-visibility-of-software-keyboard-in-android

从中选取了这段代码。

这样能一并解决以上4种情况,比较好的监听软键盘的打开和收起事件,目前正在使用中。


原文地址: http://blog.csdn.net/xcookies/article/details/43024911