android全屏添加触摸监听,Android全屏模式下软键盘监听

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);

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值