当前位置: 移动技术网 > IT编程>移动开发>Android > Android监听输入法弹窗和关闭的实现方法

Android监听输入法弹窗和关闭的实现方法

2019年07月24日  | 移动技术网IT编程  | 我要评论

独立能力,车标志图片,一枝春txt

用过ios的都知道ios上输入法关闭的同时会自动关闭输入框,那么在android上如何实现监听输入法弹出和关闭呢?本篇文章就为你提供了一种可靠的实现方式。

首先在androidmanifest中配置

android:windowsoftinputmode="adjustresize"

这样每次输入法弹出和关闭都会重新计算高度实现把布局顶上去的效果

然后我们要自定义一个布局,监听布局大小变化

public class checksoftinputlayout extends framelayout {
private onresizelistener monresizelistener;
public checksoftinputlayout(context context) {
super(context);
}
public checksoftinputlayout(context context, attributeset attrs) {
super(context, attires);
}
public checksoftinputlayout(context context, attributeset attrs, int 
defstyleattr) {
super(context, attrs, defstyleattr);
}
@targetapi(21)
public checksoftinputlayout(context context, attributeset attrs, int
defstyleattr, int defstyleres) {
super(context, attrs, defstyleattr, defstyleres);
}
@override
protected void onsizechanged(int w, int h, int oldw, int oldh) {
super.onsizechanged(w, h, oldw, old);
if (monresizelistener != null) {
monresizelistener.onresize(w, h, oldw, old);
}
}
public void setonresizelistener(onresizelistener listener) { 
this.monresizelistener = listener;
}
public interface onresizelistener {
void onresize(int w, int h, int oldw, int old);
}
}

然后把上面的自定义布局作为跟布局放到你需要的activity中去,然后在activity中绑定监听事件

mrootlayout.setonresizelistener(this);
@override
public void onresize(int w, int h, int oldw, int oldh) {
//如果第一次初始化
if (oldh == 0) {
return;
}
//如果用户横竖屏转换
if (w != oldw) {
return;
}
if (h < oldh) {
//输入法弹出
} else if (h > oldh) {
//输入法关闭
setcommentviewenabled(false, false);
}
int distance = h - old;
eventbus.getdefault().post(new inputmethodchangeevent(distance,mcurrentimageid));
}

这样只要输入法弹出和关闭就能自动实现监听,达到关闭输入框的效果,这样就和苹果的体验很一致。 到这里就介绍完了,如果有什么好的思路,也欢迎评论分享点赞! [github demo地址](https://github.com/gupengcheng/checksoftinputdemo)

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网