欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

android软键盘弹出隐藏的监听

程序员文章站 2022-04-20 08:31:42
...

通过网上搜索关于软键盘的隐藏弹出的监听,有几种方式,其中最有效的方式是在View的Onlayout()里面做文章

具体代码:

将布局视图自定义,重写onlayout()方法,然后在主Activity里面调用接口就可以了

/**
 * 为了监听软键盘的弹出隐藏
 * 
 */
public class ResizeLayout extends LinearLayout {
	private InputListener mListener;
	
	public interface InputListener {
		void OnInputListener(boolean isHideInput);
	}
	
	public void setOnResizeListener(InputListener l) {
		mListener = l;
	}
	
	public ResizeLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	
	private boolean mHasInit = false;
	private boolean mHasKeyboard = false;
	private int mHeight;
	
	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		// TODO Auto-generated method stub
		super.onLayout(changed, l, t, r, b);
		if (!mHasInit) {
			mHasInit = true;
			mHeight = b;
			System.out.println("mHeight= " + b);
		}
		else {
			mHeight = mHeight < b ? b : mHeight;
		}
		
		if (mHasInit && mHeight > b) { // mHeight代表键盘的真实高度 ,b代表在窗口中的高度 mHeight>b
			mHasKeyboard = true;
			mListener.OnInputListener(false);
		}
		if (mHasInit && mHasKeyboard && mHeight == b) { // mHeight = b
			mHasKeyboard = false;
			mListener.OnInputListener(true);
		}
	}