美文网首页
android 监听软键盘打开收起事件(软键盘自带收起按钮)

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

作者: tigg | 来源:发表于2017-04-12 17:14 被阅读1166次
    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();  
                }  
            }  
        }  
    }  
    

    代码的调用 popup_view = rootview

     SoftKeyboardStateHelper softKeyboardStateHelper = new SoftKeyboardStateHelper(popup_view);
            softKeyboardStateHelper.addSoftKeyboardStateListener(new SoftKeyboardStateHelper.SoftKeyboardStateListener() {
                @Override
                public void onSoftKeyboardOpened(int keyboardHeightInPx) {
    
                }
                @Override
                public void onSoftKeyboardClosed() {
                    popup_view.requestFocus();
                }
            });
    

    stackoverflow : http://stackoverflow.com/questions/2150078/how-to-check-visibility-of-software-keyboard-in-android
    原文连接:http://blog.csdn.net/xcookies/article/details/43024911#

    第二种实现方法

    //该Activity的最外层Layout
    finalView activityRootView = findViewById(R.id.activityRoot);
     
    //给该layout设置监听,监听其布局发生变化事件
    activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(newOnGlobalLayoutListener(){
    
        @Override
        publicvoid onGlobalLayout(){
     
           //比较Activity根布局与当前布局的大小
            int heightDiff = activityRootView.getRootView().getHeight()- activityRootView.getHeight();
            if(heightDiff >100){
            //大小超过100时,一般为显示虚拟键盘事件
                 }else{
            //大小小于100时,为不显示虚拟键盘或虚拟键盘隐藏
           }
         }
    });
    

    相关文章

      网友评论

          本文标题:android 监听软键盘打开收起事件(软键盘自带收起按钮)

          本文链接:https://www.haomeiwen.com/subject/blwmattx.html