zoukankan      html  css  js  c++  java
  • Android 软键盘的监听(监听高度,是否显示)

    Android官方本身没有提供一共好的方法来对软键盘进行监听,但我们实际应用时。非常多地方都须要针对软键盘来对UI进行一些优化。

    下面是整理出来的一个不错的方法。大家能够使用。

    public class SoftKeyboardUtil {
    	public static void observeSoftKeyboard(Activity activity, final OnSoftKeyboardChangeListener listener) {
    		final View decorView = activity.getWindow().getDecorView();
    		decorView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                int previousKeyboardHeight = -1;
    			@Override
    			public void onGlobalLayout() {
    				Rect rect = new Rect();
    				decorView.getWindowVisibleDisplayFrame(rect);
    				int displayHeight = rect.bottom - rect.top;
    				int height = decorView.getHeight();
                    int keyboardHeight = height - displayHeight;
                    if (previousKeyboardHeight != keyboardHeight) {
                        boolean hide = (double) displayHeight / height > 0.8;
                        listener.onSoftKeyBoardChange(keyboardHeight, !hide);
                    }
    
                    previousKeyboardHeight = height;
    
    			}
    		});
    	}
    
    	public interface OnSoftKeyboardChangeListener {
    		void onSoftKeyBoardChange(int softKeybardHeight, boolean visible);
    	}
    }




  • 相关阅读:
    XJOI网上同步训练DAY2 T2
    XJOI网上同步训练DAY2 T1
    BZOJ 2661 连连看
    HDU 4411 Arrest
    BZOJ 2324 营救皮卡丘
    BZOJ 1927 星际竞速
    BZOJ 3550 Vacation
    XJOI网上同步训练DAY1 T3
    php 类的相互访问
    ThinkPhp5.0_文件上传
  • 原文地址:https://www.cnblogs.com/brucemengbm/p/7261706.html
Copyright © 2011-2022 走看看