安卓监听输入法键盘打开关闭(兼容虚拟按键栏的手机,比如华为)

可以监听输入法键盘的打开和关闭动作,通过回调精准判断,兼容几乎所有手机

原理:

在输入法打开和关闭的时候会引起当前界面的高度变化,从而出发onLayout方法,所以我们监听layout动作,当触发此方法时,判断当前界面可视部分的高度变化
  • 1
  • 2
package com.shixi.didihr.utils;

import android.app.Activity;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.ViewTreeObserver;

import java.lang.reflect.Method;

/**
 * Created by Miller Zhang  on 2016/12/27.
 * desc:
 * github:https://github.com/zxyaust  CSDN:http://blog.csdn.net/qq_31340657
 * Whatever happens tomorrow,we've had today.
 */

public class ScreenUtil {

    private final Activity activity;
    private OnInputActionListener listener;

    public ScreenUtil(Activity activity) {
        this.activity = activity;
    }

    /**
     * @return 底部的虚拟栏的高度
     */
    public int getBottomKeyboardHeight() {
        int screenHeight = getAccurateScreenDpi()[1];
        DisplayMetrics dm = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);//去除底部虚拟栏之后的metric
        int heightDifference = screenHeight - dm.heightPixels;
        return heightDifference;
    }

    /**
     * 获取实际的屏幕尺寸,所有的连同底部虚拟栏
     */
    public int[] getAccurateScreenDpi() {
        int[] screenWH = new int[2];
        Display display = activity.getWindowManager().getDefaultDisplay();
        DisplayMetrics dm = new DisplayMetrics();
        try {
            Class<?> c = Class.forName("android.view.Display");
            Method method = c.getMethod("getRealMetrics", DisplayMetrics.class);
            method.invoke(display, dm);
            screenWH[0] = dm.widthPixels;
            screenWH[1] = dm.heightPixels;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return screenWH;
    }

    /**
     * 监听某个页面上输入法键盘打开动作
     *
     * @param view     要实现监听的页面上的任意一个view都可以
     * @param listener 监听接口
     */
    public void observeInputlayout(final View view, OnInputActionListener listener) {
        this.listener = listener;
        view.postDelayed(new Runnable() {
            @Override
            public void run() {
                observe(view);
            }
        }, 500);
    }


    private void observe(View view) {
        view.getViewTreeObserver().addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener() {
                    //当键盘弹出隐藏的时候会 调用此方法。
                    @Override
                    public void onGlobalLayout() {
                        Rect r = new Rect();
                        //获取当前界面可视部分
                        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
                        //获取屏幕的高度
                        int screenHeight = activity.getWindow().getDecorView().getRootView().getHeight();
                        //此处就是用来获取键盘的高度的, 在键盘没有弹出的时候 此高度为0 键盘弹出的时候为一个正数
                        int heightDifference = screenHeight - r.bottom;
                        if (heightDifference > getBottomKeyboardHeight()) {//有些手机用的是底部虚拟键,所以要大于虚拟键的高度
                            listener.onOpen();
                        } else {
                            listener.onClose();
                        }

                    }

                });
    }

    public interface OnInputActionListener {
        void onOpen();

        void onClose();

    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107

使用方法:

把上面的类复制到自己项目里面,然后调用下面方法:
  • 1
  • 2
new ScreenUtil(getActivity()).observeInputlayout(view, new ScreenUtil.OnInputActionListener() {
            @Override
            public void onOpen() {
                startAniout();
            }

            @Override
            public void onClose() {
                startAniIn();
            }
        });
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在华为手机上使用 Charles 进行抓包,需要进行以下步骤: 1. 首先,在华为手机上下载安装 Charles Proxy 的安装包,并安装 Charles Proxy 应用。 2. 打开 Charles Proxy 应用,并进入菜单的 Proxy > Proxy Settings。 3. 在 Proxy Settings 中,找到 Proxy Listeners,点击 Add 按钮添加一个新的监听器。 4. 在添加监听器的界面中,设置监听的端口号为 8888,并选择 Allow Remote Computers to Connect 选项。 5. 点击 OK 按钮保存设置,然后返回 Charles Proxy 应用主界面。 6. 在 Charles Proxy 应用主界面中,点击菜单的 Help > SSL Proxying > Install Charles Root Certificate on a Mobile Device or Remote Browser。 7. 根据提示,在手机上下载并安装 Charles 的证书。 8. 在手机的设置中找到 WLAN 设置,长按当前连接的 Wi-Fi 网络,选择 Modify Network。 9. 在 Modify Network 界面中,找到 Proxy 设置,选择 Manual,将代理服务器设置为 Charles Proxy 应用运行所在的电脑的 IP 地址,端口号设置为 8888。 10. 打开手机上需要进行抓包的应用,此时 Charles Proxy 应用会自动捕获应用的网络请求,并在主界面中进行展示。 注意:在使用 Charles 进行抓包时,需要确保手机和电脑连接在同一个局域网中,且 Charles Proxy 应用在电脑上处于运行状态。同时,部分应用可能会对抓包进行防护,需要进行额外的设置才能进行抓包。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值