发布于 2017-10-03 23:55:18 | 204 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


用过ios的都知道ios上输入法关闭的同时会自动关闭输入框,那么在android上如何实现监听输入法弹出和关闭呢?接下来通过本文给大家分享一种可靠的实现方式

用过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)



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务