Giter Club home page Giter Club logo

keyboardchangelistener's People

Contributors

yescpu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

keyboardchangelistener's Issues

键盘自动弹出

我设置完这个属性之后:android:windowSoftInputMode="adjustResize"
每次一进页面键盘就会自动弹起,而我不想要这个效果(;´д`)ゞ

说没用的看这里!!!

嗯,我刚开始也觉得没用,通过打印LOG发现监听屏幕高度并没有变化。。。
就很吃🐳。
仔细想想可能是自己使用方法不对。
于是一个个试,终于 发现是自己 代码设置问题。
由于考虑到 不同机型 不同厂家的皮。

请大家 先把自己 有关所有 状态栏设置代码屏蔽后 尝试使用该库!!!
请大家 先把自己 有关所有 状态栏设置代码屏蔽后 尝试使用该库!!!
请大家 先把自己 有关所有 状态栏设置代码屏蔽后 尝试使用该库!!!

应该是 与 FULL_SCEEN_MODEL 等设置有关,具体的没时间去尝试。
如果还是没有,那就是运气不好了。
尝试其他办法吧,另外拿来主义也不要这么🐻呀,大家心平气!@¥#¥#@%@¥%@%@#%¥……#¥%#¥

adjustNothing

hi, yescpu, To use adjustNothing and to listen to the keyboard, what do you need to do with WindowManager? Is there a link that can be attached? Thank you.

楼下的小伙伴写六点口德,用了别人的东西有问题不思改进还乱骂,无语你们这些伸手党

作者的是有点问题,更改为这个就可以了:

public class CqrKeyboardListen implements ViewTreeObserver.OnGlobalLayoutListener {
    private View contentView;
    private int originVisibleH;
    private OnKeyboardListener onKeyboardListener;

    private CqrKeyboardListen(Activity activity, OnKeyboardListener listener) {
        if (activity == null) {
            return;
        }
        activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
                | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        onKeyboardListener = listener;
        contentView = activity.findViewById(android.R.id.content);
        if (contentView != null) {
            contentView.getViewTreeObserver().addOnGlobalLayoutListener(this);
        }
    }

    public static CqrKeyboardListen listen(Activity activity, OnKeyboardListener listener) {
        return new CqrKeyboardListen(activity, listener);
    }

    @Override
    public void onGlobalLayout() {
        if (onKeyboardListener == null) {
            return;
        }
        //布局变化,获取当前根视图在屏幕上显示的高度
        Rect rect = new Rect();
        contentView.getWindowVisibleDisplayFrame(rect);
        int currentVisibleH = rect.height();
        if (originVisibleH == 0) {
            originVisibleH = currentVisibleH;
        }
        int diffHeight = originVisibleH - currentVisibleH;
        //CqrLogPrinter.printLog("originVisibleH=" + originVisibleH + ",currentVisibleH="
        //        + currentVisibleH + ",diffHeight=" + diffHeight);
        if (diffHeight > 200) {
            //根视图显示高度变小超过200,可以看作软键盘显示了
            onKeyboardListener.onKeyboardChange(true, diffHeight);
            originVisibleH = currentVisibleH;
            return;
        }
        if (diffHeight < -200) {
            //根视图显示高度变大超过200,可以看作软键盘隐藏了
            onKeyboardListener.onKeyboardChange(false, diffHeight);
            originVisibleH = currentVisibleH;
        }
    }

    /**
     * 在{@link Activity#onDestroy()}中调用,防止内存泄漏
     */
    public void destroy() {
        if (contentView != null) {
            CqrCompatUtils.removeOnGlobalLayoutListener(contentView, this);
        }
    }

    public interface OnKeyboardListener {
        void onKeyboardChange(boolean isShow, int keyboardHeight);
    }

}

使用:

     keyboardListen = CqrKeyboardListen.listen(activity, new CqrKeyboardListen.OnKeyboardListener() {
            @Override
            public void onKeyboardChange(boolean isShow, int keyboardHeight) {
                // DO SOMETHING
            }
        });
    @Override
    protected void onDestroy() {
        super.onDestroy();
        keyboardListen.destroy();
    }

由于键盘类型不同,高度不同所导致的切换键盘的时候,没有回调

if (mShowFlag != currentShow) {
mShowFlag = currentShow;
mKeyboardListener.onKeyboardChange(currentShow, keyboardHeight);
}

这里的回调仅仅当 mShowFlag != currentShow 条件成立的时候才会回调,这里其实是有问题的。

  • 我自己使用的环境是登录界面,该界面存在至少 2 个以上的 EditText,而且inputType的类型不相同的时候,会出现问题,具体操作如下:
    • 当我在登录界面的时候,上下两个连续的EditText,一个 EditTextinputTypephone,另外一个inputTypetextPassword.
    • 首先,我点击了第一个输入手机号的EditText,弹出只能输入数字的键盘,他的高度在我的手机上是691 像素
    • 输入完之后,我点击输入密码的EditText, 会弹出可以同时输入账号和密码的键盘,高度为912像素

为了方便测试,我自己在你的基础上添加了额外的测试信息

override fun onGlobalLayout() {
        ...
        Log.d(TAG, "onGlobalLayout() called  screenHeight $screenHeight VisibleDisplayHeight $windowBottom, keyboardHeight: $keyboardHeight")
        val currentShow = keyboardHeight > MIN_KEYBOARD_HEIGHT
        Log.d(TAG, "mShowFlag: $mShowFlag, currentSHow: $currentShow")
        if (mShowFlag != currentShow) {
            mShowFlag = currentShow
            keyboardListener.onKeyboardChange(currentShow, keyboardHeight)
        }
    }
  • 以上的操作流程,正常情况下,输出的日志应该如下
    • 正常情况下,键盘切换会经历 旧键盘隐藏 -> 新键盘弹出的一个过程
    • 这种情况下,切换不同高度的键盘,也可以正常回调onKeyboardChange方法,因为mShowFlag != currentShow
// 这个是第一次点击输入手机号的时候
 D/KeyboardChangeListener: onGlobalLayout() called  screenHeight 1920 VisibleDisplayHeight 1229, keyboardHeight: 691
 D/KeyboardChangeListener: mShowFlag: false, currentShow: true

// 这个是第二次点击输入密码 EditText的时候,正常情况,每切换一次,会出现 3 对日志(共6条)
D/KeyboardChangeListener: onGlobalLayout() called  screenHeight 1920 VisibleDisplayHeight 1229, keyboardHeight: 691
D/KeyboardChangeListener: mShowFlag: true, currentShow: true
D/KeyboardChangeListener: onGlobalLayout() called  screenHeight 1920 VisibleDisplayHeight 1920, keyboardHeight: 0
D/KeyboardChangeListener: mShowFlag: true, currentShow: false
D/KeyboardChangeListener: onGlobalLayout() called  screenHeight 1920 VisibleDisplayHeight 1008, keyboardHeight: 912
D/KeyboardChangeListener: mShowFlag: false, currentShow: true
  • 然后,当我在两者之间进行反复切换的时候,会出现一种异常情况(小概率出现),没有回调onKeyboardChange方法。
    • 根据打印可以知道, 两条日志都是mShowFlag = true, currentShow = true,但是键盘的高度是不同的,从这里也可以得出解决方法。
// 异常情况,只会出现 2 对日志(共 4)条日志
D/KeyboardChangeListener: onGlobalLayout() called  screenHeight 1920 VisibleDisplayHeight 1008, keyboardHeight: 912
D/KeyboardChangeListener: mShowFlag: true, currentShow: true
D/KeyboardChangeListener: onGlobalLayout() called  screenHeight 1920 VisibleDisplayHeight 1229, keyboardHeight: 691
D/KeyboardChangeListener: mShowFlag: true, currentShow: true
  • 目前我的解决方法:
    • 仅仅是添加了,当键盘处于显示的时候,判断高度是否发生了改变
    • 当然更好一点的做法,可能是把高度变化单独抽离出来变为一个接口
 if (mShowFlag != currentShow || (mShowFlag && (previousKeyboardHeight != keyboardHeight))) {
            mShowFlag = currentShow
            keyboardListener.onKeyboardChange(currentShow, keyboardHeight)
        }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.