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

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时,为不显示虚拟键盘或虚拟键盘隐藏
       }
     }
});
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,708评论 2 45
  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的阅读 14,597评论 5 6
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,764评论 25 709
  • 爸:哎!帮我倒杯绿豆水。 妈:我手脏,自己去。 爸:唉呀! 妈:都说我手脏了! 爸:我是喝绿豆水又不是喝你手。
    掉线黑眼圈阅读 1,345评论 0 0
  • 时间完美的定格在这里, 不需要生气, 更不要哭泣, 因为我从未得到你。 缘分的迷离, 爱情的可惜, 仿佛一切都那样...
    Jenuary阅读 3,793评论 2 5