Android全屏软键盘监听
程序员文章站
2022-04-12 09:13:43
Android开发的同学都知道,Android系统是不提供所谓的软键盘监听的,只提供了开关软键盘的相关api 如下 /** * 开启软键盘 * @param activity */ public static void showKeybord(Activity activity,EditText et) { InputMethodManager imm = (InputMethodManager)activity.getSystemService(C...
Android开发的同学都知道,Android系统是不提供所谓的软键盘监听的,只提供了开关软键盘的相关api 如下
/**
* 开启软键盘
* @param activity
*/
public static void showKeybord(Activity activity,EditText et) {
InputMethodManager imm = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm != null) {
imm.showSoftInput(et, 0);
}
}
/**
* 关闭软键盘
* @param activity
*/
public static void closeKeybord(Activity activity) {
InputMethodManager imm = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm != null) {
imm.hideSoftInputFromWindow(activity.getWindow().getDecorView().getWindowToken(), 0);
}
}
当开发使用的是Activity主题(Theme)是非全屏主题(fullscreen)非沉浸式的时候,可以通过DecordView 获取ViewTreeObserve去监听Layout变化,但是,如果当我们使用全屏主题的时候就会发现监听没任何作用了,网上抄的那些Utils也全都失效了
那我们能否在全屏的时候监听到软键盘的状态,以符合我们的开发业务的需求呢?
答案是肯定的
我们可以通过添加一个PopWindow 来解决无法监听软键盘的问题,直接上代码
public class FullScreenKeyboardHeightProvider extends PopupWindow implements OnGlobalLayoutListener {
private Activity mActivity;
private View mRootView;
private HeightListener mHeightListener;
private int mHeightMax; // 记录popup内容区的最大高度
private int mKeyboardHeight;
public FullScreenKeyboardHeightProvider(Activity activity) {
super(activity);
this.mActivity = activity;
// 基础配置
mRootView = new View(activity);
setContentView(mRootView);
// 监听全局Layout变化
mRootView.getViewTreeObserver().addOnGlobalLayoutListener(this);
setBackgroundDrawable(new ColorDrawable(0));
// 设置宽度为0,高度为全屏
setWidth(0);
setHeight(LayoutParams.MATCH_PARENT);
// 设置键盘弹出方式
setSoftInputMode(LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
}
public FullScreenKeyboardHeightProvider init() {
if (!isShowing()) {
final View view = mActivity.getWindow().getDecorView();
// 延迟加载popupwindow,如果不加延迟就会报错
view.post(new Runnable() {
@Override
public void run() {
showAtLocation(view, Gravity.NO_GRAVITY, 0, 0);
}
});
}
return this;
}
public FullScreenKeyboardHeightProvider setHeightListener(HeightListener listener) {
this.mHeightListener = listener;
return this;
}
@Override
public void onGlobalLayout() {
Rect rect = new Rect();
mRootView.getWindowVisibleDisplayFrame(rect);
if (rect.bottom > mHeightMax) {
mHeightMax = rect.bottom;
}
// 两者的差值就是键盘的高度
int currentKeyboardHeight = mHeightMax - rect.bottom;
if (currentKeyboardHeight==mKeyboardHeight){
return;
}
//
if (mHeightListener != null) {
ValueAnimator valueAnimator = ValueAnimator.ofInt(mKeyboardHeight, currentKeyboardHeight);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int animatedValue = (int) animation.getAnimatedValue();
mHeightListener.onHeightChanged(animatedValue);
}
});
valueAnimator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
mKeyboardHeight = currentKeyboardHeight;
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
mHeightListener.onHeightChanged(mKeyboardHeight);
valueAnimator.setDuration(200);
valueAnimator.setInterpolator(new DecelerateInterpolator());
valueAnimator.start();
}
}
public interface HeightListener {
void onHeightChanged(int height);
}
}
demo下载地址
本文地址:https://blog.csdn.net/qq_35959231/article/details/107401550
推荐阅读