[經典技巧]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;

        }

    }

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章