首页 > 其他分享 > Android软键盘弹出关闭监听

Android软键盘弹出关闭监听

时间:2023-09-14 16:13:46浏览次数:51  
标签:activityRootView heightDiff void isSoftKeyboardOpened 监听 listener 软键盘 Android pu

https://juejin.cn/post/6844903489051557902?from=singlemessage&isappinstalled=0#comment

package com.xiucai.common.manager;

import android.graphics.Rect;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by SuperD on 2017/5/12.
 */

public class SoftKeyBroadManager  implements ViewTreeObserver.OnGlobalLayoutListener{

    public interface SoftKeyboardStateListener {
        void onSoftKeyboardOpened(int keyboardHeightInPx);

        void onSoftKeyboardClosed();
    }

    private final List<SoftKeyboardStateListener> listeners = new LinkedList<SoftKeyboardStateListener>();
    private final View activityRootView;
    private int lastSoftKeyboardHeightInPx;
    private boolean isSoftKeyboardOpened;

    public SoftKeyBroadManager(View activityRootView) {
        this(activityRootView, false);
    }

    public SoftKeyBroadManager(View activityRootView, boolean isSoftKeyboardOpened) {
        this.activityRootView = activityRootView;
        this.isSoftKeyboardOpened = isSoftKeyboardOpened;
        activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(this);
    }

    @Override
    public void onGlobalLayout() {
        final Rect r = new Rect();
        //r will be populated with the coordinates of your view that area still visible.
        activityRootView.getWindowVisibleDisplayFrame(r);

        final int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);
        Log.d("SoftKeyboardStateHelper", "heightDiff:" + heightDiff);
        if (!isSoftKeyboardOpened && heightDiff > 500) { // if more than 100 pixels, its probably a keyboard...
            isSoftKeyboardOpened = true;
            notifyOnSoftKeyboardOpened(heightDiff);
            //if (isSoftKeyboardOpened && heightDiff < 100)
        } else if (isSoftKeyboardOpened && heightDiff < 500) {
            isSoftKeyboardOpened = false;
            notifyOnSoftKeyboardClosed();
        }
    }

    public void setIsSoftKeyboardOpened(boolean isSoftKeyboardOpened) {
        this.isSoftKeyboardOpened = isSoftKeyboardOpened;
    }

    public boolean isSoftKeyboardOpened() {
        return isSoftKeyboardOpened;
    }

    /**
     * Default value is zero (0)
     *
     * @return last saved keyboard height in px
     */
    public int getLastSoftKeyboardHeightInPx() {
        return lastSoftKeyboardHeightInPx;
    }

    public void addSoftKeyboardStateListener(SoftKeyboardStateListener listener) {
        listeners.add(listener);
    }

    public void removeSoftKeyboardStateListener(SoftKeyboardStateListener listener) {
        listeners.remove(listener);
    }

    private void notifyOnSoftKeyboardOpened(int keyboardHeightInPx) {
        this.lastSoftKeyboardHeightInPx = keyboardHeightInPx;

        for (SoftKeyboardStateListener listener : listeners) {
            if (listener != null) {
                listener.onSoftKeyboardOpened(keyboardHeightInPx);
            }
        }
    }

    private void notifyOnSoftKeyboardClosed() {
        for (SoftKeyboardStateListener listener : listeners) {
            if (listener != null) {
                listener.onSoftKeyboardClosed();
            }
        }
    }
}

不管是全屏或者其他情况,使用这个工具类来监听软键盘就可以了,具体的用法:

SoftKeyBroadManager mManager =new SoftKeyBroadManager ("根布局");
//添加软键盘的监听,然后和上面一样的操作即可.
mSoftKeyBroadManager.addSoftKeyboardStateListener();
//注意销毁时,得移除监听
mSoftKeyBroadManager.removeSoftKeyboardStateListener();

 

标签:activityRootView,heightDiff,void,isSoftKeyboardOpened,监听,listener,软键盘,Android,pu
From: https://www.cnblogs.com/zuiniub/p/17702760.html

相关文章

  • Android Studio中无法显示main.dart(Flutter项目在Android Studio中显示不全)
    问题描述创建完项目后只出现android文件选择ProjectFiles就会显示整个目录内容设置后......
  • android短视频开发,scroll-view的横向滚动
    android短视频开发,scroll-view的横向滚动css .scrollCon{white-space:nowrap;display:flex;align-items:center;}.monthItem{display:inline-block;width:calc(100%/6);font-size:26rpx;color:#3D3D3D;text-align:center;}​总结: 核心css关键点 scroll-view......
  • Android inject详解
    本篇Blog源于我在上一家互联网公司工作中的一项任务,前几天原来公司的一个同事让我整理个文档出来学习一下。今天写完文档后我决定再分享到Blog上一份。希望对需要的人有所帮助,或者能够激发读者的创意。作者shensy----------------------------------------------------------------......
  • android中使用greedDao
     android中使用greedDao一工具说明Greendao是一个在android中快速生成数据库操作的orm工具,最近在项目中使用到,具体操作记录如下,供以后使用快速集成。该项目的源码在gitee的镜像地址为:【https://gitee.com/freewsf/greenDAO_1#add-greendao-to-your-project】二使用方式1.在......
  • Android-2-manifest和build.gradle两个关键文件
    Androidmanifest.xml本质上就是用res中的文件配置项目的情况,像使用到的权限,app的名字,icon等等build.gradle.kts本质上和maven没什么区别,主要是做两个事情,一是添加插件,二是添加库(检查有无这个库,没有的话就从网上下载)Androidmanifest.xml<?xmlversion="1.0"encoding="utf-8"?>......
  • android-1-入门
    环境配置下载正版androidstudio使用即可在androidstudio初始化第一次运行的需要注意的是三个东西是需要下载的androidapi33就是app程序编译成apk的sdk对应android9.0以上的版本的运行环境gradle一般自动下载,但可能失败手动下载:couldnotinstallgradledistribut......
  • Android 编译线程爆了, gradle 内存 OOM 解决之路
    本文首发我的微信公众号徐公,收录于Github·AndroidGuide,这里有Android进阶成长知识体系,希望我们能够一起学习进步,关注公众号徐公,5年中大厂程序员,一起建立核心竞争力背景最近我们项目在编译的时候,编译多次之后,有挺多人反馈会出现OOM的,在项目的根目录下面会出现hs_err_......
  • 爆肝总结2023Android面试,看完学会它,公司追着给你offer
    前言想要在面试中脱颖而出吗?想要在最短的时间内快速掌握Android的核心知识点吗?想要成为一位优秀的Android工程师吗?本篇文章能助你一臂之力!金九银十,目前正值招聘求职旺季,很多朋友对一些新技术名词都能侃侃而谈,但对一些核心原理理解的不够透彻,特别是对Android的一些核心基础知识点掌......
  • android 手机开发虚拟定位
    Android系统提供了模拟位置提供者(MockLocationProvider)来允许开发者模拟虚拟位置信息,用于测试应用的地理位置相关功能。1.获取模拟位置权限:在你的应用的AndroidManifest.xml文件中添加以下权限<uses-permissionandroid:name="android.permission.ACCESS_MOCK_LOCATION"/>2.启用......
  • 对标金九银十,分享32个模块的Android面试题,分分钟拿捏面试官
    前言2023年初伴随着疫情结束,迎来了“金三银四”。以为终于迎来胜利的“曙光”,不成想,现实却是当头一棒!!!从“金三银四”的“战绩”来看,程序员跳槽或者找工作并不理想,大批人迟迟找不到工作,大厂仍旧在进行几轮裁员,整个就业市场都不是太好!出现这种情况是因为中美贸易战,导致大环境不好、大......