android软键盘现实和隐藏的监听

来源:互联网 发布:nba球员体测数据排行 编辑:程序博客网 时间:2024/05/01 13:35

http://stackoverflow.com/questions/4312319/howto-capture-the-virtual-keyboard-show-hide-event-in-android


http://stackoverflow.com/questions/2150078/how-to-check-visibility-of-software-keyboard-in-android?lq=1


大概的方法就是监听view的的高度的变化,来判断软键盘的显示和隐藏。

注意:一点就是属性要设置的好,很多情况下试adjustResize才有效。

import java.util.Arraylist;import android.content.Context;import android.util.AttributeSet;import android.widget.RelativeLayout;public class KeyboardDetectorRelativeLayout extends RelativeLayout {    public interface IKeyboardChanged {        void onKeyboardShown();        void onKeyboardHidden();    }    private ArrayList<IKeyboardChanged> keyboardListener = new ArrayList<IKeyboardChanged>();    public KeyboardDetectorRelativeLayout(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);    }    public KeyboardDetectorRelativeLayout(Context context, AttributeSet attrs) {        super(context, attrs);    }    public KeyboardDetectorRelativeLayout(Context context) {        super(context);    }    public void addKeyboardStateChangedListener(IKeyboardChanged listener) {        keyboardListener.add(listener);    }    public void removeKeyboardStateChangedListener(IKeyboardChanged listener) {        keyboardListener.remove(listener);    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        final int proposedheight = MeasureSpec.getSize(heightMeasureSpec);        final int actualHeight = getHeight();        if (actualHeight > proposedheight) {            notifyKeyboardShown();        } else if (actualHeight < proposedheight) {            notifyKeyboardHidden();        }        super.onMeasure(widthMeasureSpec, heightMeasureSpec);    }    private void notifyKeyboardHidden() {        for (IKeyboardChanged listener : keyboardListener) {            listener.onKeyboardHidden();        }    }    private void notifyKeyboardShown() {        for (IKeyboardChanged listener : keyboardListener) {            listener.onKeyboardShown();        }    }}


0 0
原创粉丝点击