码迷,mamicode.com
首页 > 移动开发 > 详细

android监听键盘

时间:2016-01-11 10:21:52      阅读:460      评论:0      收藏:0      [点我收藏+]

标签:

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就可以处理键盘事件了。

参考文档:

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

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

 

 






android监听键盘

标签:

原文地址:http://www.cnblogs.com/permanent2012moira/p/5120282.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!