zoukankan      html  css  js  c++  java
  • android监听键盘

    android中的带有输入功能的页面布局经常被弹出的键盘遮挡,一种处理方法是监听键盘的弹出,设置布局的padding或隐藏某些占位控件,使得输入框不被键盘遮挡。一种常用的方法是当Activity设置为android:windowSoftInputMode="adjustResize"的时候,键盘的弹出和隐藏会触发onSizeChaged的事件,用下面的自定义RealativeLayout则能监听这个函数并判断键盘是弹出还是收起。

    /*
     * @Project: GZJK
     * @Author: cmcc
     * @Date: 2015年5月6日
     * @Copyright: 2000-2015 CMCC . All rights reserved.
     */
    package com.matrix.appsdk.widget.keyboard;
    
    import android.app.Activity;
    import android.content.Context;
    import android.util.AttributeSet;
    import android.view.Display;
    import android.widget.RelativeLayout;
    
    /** 
    * @ClassName: InputMethodRelativeLayout 
    * @Description: 监听键盘的现实和隐藏,Activity设置为adjustResize,非全屏
    * @author
    * @date 2015年5月6日 下午6:48:57 
    */
    public class InputMethodRelativeLayout extends RelativeLayout {
        private int width;
        protected OnSizeChangedListenner onSizeChangedListenner;
        private boolean sizeChanged = false; // 变化的标志
        private int height;
        private int screenHeight; // 屏幕高度
        
        public InputMethodRelativeLayout(Context paramContext, AttributeSet paramAttributeSet) {
            super(paramContext, paramAttributeSet);
            Display localDisplay = ((Activity)paramContext).getWindowManager().getDefaultDisplay();
            this.screenHeight = localDisplay.getHeight();
        }
        
        public InputMethodRelativeLayout(Context paramContext, AttributeSet paramAttributeSet, int paramInt) {
            super(paramContext, paramAttributeSet, paramInt);
        }
        
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            this.width = widthMeasureSpec;
            this.height = heightMeasureSpec;
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    
        }
        
        @Override
        public void onSizeChanged(int w, int h, int oldw, int oldh) {
            // 监听不为空、宽度不变、当前高度与历史高度不为0
            if ((this.onSizeChangedListenner != null) && (w == oldw) && (oldw != 0) && (oldh != 0)) {
                if ((h >= oldh) || (Math.abs(h - oldh) <= 1 * this.screenHeight / 4)) {
                    if ((h <= oldh) || (Math.abs(h - oldh) <= 1 * this.screenHeight / 4))
                        return;
                    this.sizeChanged = false;
                } else {
                    this.sizeChanged = true;
                }
                this.onSizeChangedListenner.onSizeChange(this.sizeChanged, oldh, h);
                measure(this.width - w + getWidth(), this.height - h + getHeight());
            }
        }
    
    
        /**
         * 设置监听事件
         * @param paramonSizeChangedListenner
         */
        public void setOnSizeChangedListenner(InputMethodRelativeLayout.OnSizeChangedListenner paramonSizeChangedListenner) {
            this.onSizeChangedListenner = paramonSizeChangedListenner;
        }
        
        /**
         * 大小改变的内部接口
         * @author junjun
         *
         */
        public  interface OnSizeChangedListenner {
            void onSizeChange(boolean showKeyboard, int oldH, int newH);
        }
    }
    View Code

    这样,使用该自定义layout的Activity只需要在回调onSizeChange中进行处理就可以了。

    但是,当Activity设置为全屏时,即使activity属性设置为adjustResize,当键盘显示或隐藏时,这个方法也不会被触发,此时可以监听根View的变化。在Activity设置完layout后使用下面的一句话:

    AndroidBug5497Workaround.assistActivity(this);
    AndroidBug5497Workaround的代码如下:
    package com.matrix.appsdk.widget.keyboard;
    
    import android.app.Activity;
    import android.graphics.Rect;
    import android.view.View;
    import android.view.ViewTreeObserver;
    import android.widget.FrameLayout;
    
    /**
     * @author BMR
     * @ClassName: AndroidBug5497Workaround
     * @Description: 解决Activity为全屏时设置为adjustResize,InputMethodRelativeLayout检测键盘不触发onSizeChanged
     * @date 2016/1/9 21:09
     */
    
    //Workaround to get adjustResize functionality for input methos when the fullscreen mode is on
    //found by Ricardo
    //taken from http://stackoverflow.com/a/19494006
    public class AndroidBug5497Workaround {
        // For more information, see https://code.google.com/p/android/issues/detail?id=5497
        // To use this class, simply invoke assistActivity() on an Activity that already has its content view set.
    
        public static void assistActivity (Activity activity) {
            new AndroidBug5497Workaround(activity);
        }
    
        private View mChildOfContent;
        private int usableHeightPrevious;
        private FrameLayout.LayoutParams frameLayoutParams;
    
        private AndroidBug5497Workaround(Activity activity) {
            FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);
            mChildOfContent = content.getChildAt(0);
            mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                public void onGlobalLayout() {
                    possiblyResizeChildOfContent();
                }
            });
            frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();
        }
    
        private void possiblyResizeChildOfContent() {
            int usableHeightNow = computeUsableHeight();
            if (usableHeightNow != usableHeightPrevious) {
                int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();
                int heightDifference = usableHeightSansKeyboard - usableHeightNow;
                if (heightDifference > (usableHeightSansKeyboard/4)) {
                    // keyboard probably just became visible
                    frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;
                } else {
                    // keyboard probably just became hidden
                    frameLayoutParams.height = usableHeightSansKeyboard;
                }
                mChildOfContent.requestLayout();
                usableHeightPrevious = usableHeightNow;
            }
        }
    
        private int computeUsableHeight() {
            Rect r = new Rect();
            mChildOfContent.getWindowVisibleDisplayFrame(r);
            return (r.bottom - r.top);
        }
    
    }
    View Code

    这样,在利用开始的方法就可以了,onSizeChange就可以处理键盘事件了。

    上面的方法监听到键盘的隐藏和显示后,还是需要设置布局控件的隐藏,padding或者margin等属性来调节高度。还有一种不用上面自定义RelativeLayout直接让系统adjustRisize简单的方法,如果直接用EditText然后AndroidManifest.xml中设置它的Activtiy为

    android:windowSoftInputMode="stateHidden|adjustResize"

    键盘出现后会出现部分遮挡输入框,如下图:

    最简单的解决方法让遮挡变小,仿照微信的给输入文本加一个下划线,效果(下图右)就好一些。注意下划线用点9的图,编辑区域留出合适的就可以了。这是从效果图上试出来的方法,具体的键盘遮挡和显示隐藏等问题还要看sourcecode啊~~

    参考文档:

    android 软键盘在全屏下的布局计算问题

    android 软键盘的显示与隐藏问题的研究






  • 相关阅读:
    notepad++ 安装
    Git 安装
    C 字符串常量 数据类型
    vue路由传参query和params的区别
    mysql 在 centos 上安装,做成服务,且开机启动 步骤
    全网最详细Apache Kylin1.5安装(单节点)和测试案例 ---> 现在看来 kylin 需要 安装到Hadoop Master 节点上
    Kylin build cube step 2 报错(Kylin 安装在slave 节点上)
    Kylin build cube step 2 报错
    Kylin 环境 搭建 成功
    Kylin 环境 搭建 报错
  • 原文地址:https://www.cnblogs.com/permanent2012moira/p/5120282.html
Copyright © 2011-2022 走看看