[经典技巧]android 如何监听输入法是否弹出或隐藏,监听手机是否是全屏切换

思路:
1.创建一个宽度为0,高度为MATCH_PARENT的浮窗。浮窗要求能够被输入法盖住。


现象:
1.当输入法调起时,浮窗的高度会变化。变化为viewHeight=oldviewHeight-inputHeight;
2.当全屏时,浮窗的高度会变化为。viewHeight=screenHeight;

3.高度变化时,会回调onSizeChanged方法。所以在onSizeChanged中就可以进行监听


直接上代码:

package com.milo.test;

 

import android.content.Context;

import android.content.res.Configuration;

import android.graphics.PixelFormat;

import android.util.DisplayMetrics;

import android.util.Log;

import android.view.Gravity;

import android.view.View;

import android.view.WindowManager;

 

public class FloatKeyboardMonitor extends View{

    private static final boolean DEBUG = true;

    private static final String TAG ="float.KeyboardMonitor";

   

    private final WindowManagerwindowManager;

    private final WindowManager.LayoutParamslayoutParams;

   

    private int screenHeight = 0;

    private int softKeyboardHeight = 0;

    private int oldOrientation=0;

 

    public FloatKeyboardMonitor(Context context){

        super(context);

        windowManager =((WindowManager)context.getSystemService("window"));

        layoutParams = newWindowManager.LayoutParams();       

        layoutParams.width= 0;

        layoutParams.x=0;

        layoutParams.height =WindowManager.LayoutParams.MATCH_PARENT;

        layoutParams.type =WindowManager.LayoutParams.TYPE_PHONE;

        //关键是WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM

        //要求能够被输入法遮挡

        layoutParams.flags= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM|WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;

        layoutParams.format= PixelFormat.TRANSPARENT;

        layoutParams.gravity= Gravity.LEFT | Gravity.TOP;

       windowManager.addView(this, layoutParams);

       setScreenHeight(context);

        setSoftKeyboardHeight(context);

    }   

    private void setScreenHeight(Contextcontext){

        DisplayMetrics dm =context.getResources().getDisplayMetrics();

        screenHeight =dm.heightPixels;//手机屏幕高度

    } 

    private void setSoftKeyboardHeight(Contextcontext){

        DisplayMetrics dm =context.getResources().getDisplayMetrics();

        softKeyboardHeight =(int)(dm.density*100f+0.5f);//软键盘的高度,这里定义了一个随机值,假设所有手机输入法最小高度为100dp

    } 

    @Override

    protected void onSizeChanged(int w, int h,int oldw, int oldh) {

       super.onSizeChanged(w, h, oldw, oldh);

        if (DEBUG) {

           Log.i(TAG, "screenHeight=" + screenHeight + ";w=" + w +";h=" + h + ";oldw=" + oldw + ";oldh=" + oldh);

        }

        if (h ==screenHeight) {

           if (oldh != 0) {

               if (DEBUG) {

                   Log.i(TAG, "变化为全屏了.");

               }   

            }else{

               if (DEBUG) {

                   Log.i(TAG, "初始化,当前为全屏状态.");

               }

           }           

        } else if(Math.abs(h - oldh) > softKeyboardHeight) {

           if (h >= oldh) {

               if (DEBUG) {

                   Log.i(TAG, "变化为正常状态(输入法关闭).");

               }

           }else{

               if (DEBUG) {

                   Log.i(TAG, "输入法显示了.");

               }

           }          

        } else{

           if (oldh != 0) {

               if (DEBUG) {

                   Log.i(TAG, "变化为正常状态.(全屏关闭)");

               }

           }else{

               if (DEBUG) {

                   Log.i(TAG, "初始化,当前为正常状态.");

               }

           }

        }

    }

 

    @Override

    protected voidonConfigurationChanged(Configuration newConfig) {

       super.onConfigurationChanged(newConfig);

        if (DEBUG) {

           Log.i(TAG, "onConfigurationChanged neworientation=" +newConfig.orientation + ";oldOrientation=" + oldOrientation);

        }

        if (oldOrientation !=newConfig.orientation) {

           setScreenHeight(getContext());

           oldOrientation = newConfig.orientation;

        }

    }

}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值