android监听键盘的隐藏,Android监听软键盘的显示和隐藏

使用步骤

xml 布局文件布局,和普通的控件一下

获取SoftInputCanListenerEditText 实例,并设置监听器

Activity 注册的时候android:windowSoftInputMode 使用默认值,就是说不要写这个就对了

/**

* author: vector.huang

* date: 2016/10/12 19:07

*/

public class SoftInputCanListenerEditText extends AppCompatEditText {

private OnSoftInputChangeListener mOnSoftInputChangeListener;

private int mPreHeight; //上一次计算时的高度

private View mContentView;

private boolean isShow;

public SoftInputCanListenerEditText(Context context) {

super(context);

}

public SoftInputCanListenerEditText(Context context, AttributeSet attrs) {

super(context, attrs);

}

public SoftInputCanListenerEditText(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

}

public void setOnSoftInputChangeListener(OnSoftInputChangeListener onSoftInputChangeListener) {

mOnSoftInputChangeListener = onSoftInputChangeListener;

}

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

if (isInEditMode()) {

return;

}

if (mContentView == null) {

Activity activity = (Activity) ((TintContextWrapper) getContext()).getBaseContext();

mContentView = activity.findViewById(android.R.id.content);

}

int nowHeight = mContentView.getHeight();

if (mPreHeight == 0) {

//第一次进入

mPreHeight = nowHeight;

return;

}

if (mPreHeight == nowHeight) {

//同样的不处理

return;

}

if (mOnSoftInputChangeListener != null && nowHeight < mPreHeight) {

//变小了之后就是显示了键盘

mOnSoftInputChangeListener.onChange(true);

isShow = true;

//显示键盘之后就开始检查什么时候收起

startCheck();

mPreHeight = nowHeight;

}

}

private Runnable check = () -> {

int nowHeight = mContentView.getHeight();

if (mOnSoftInputChangeListener != null && nowHeight > mPreHeight) {

//变小了之后就是显示了键盘

mOnSoftInputChangeListener.onChange(false);

isShow = false;

mPreHeight = nowHeight;

return;

}

//键盘没有收起,接着检查

startCheck();

};

//定时检查才行呀

private void startCheck() {

postDelayed(check, 100);

}

public boolean isShow() {

return isShow;

}

public interface OnSoftInputChangeListener{

void onChange(boolean isShow);

}

}

感谢支持,更多请看把时间当初朋友

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值