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/19494006public 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 软键盘的显示与隐藏问题的研究






更多相关文章

  1. Android(安卓)Activity 详解
  2. Android开发艺术探索知识回顾——第2章 IPC机制:1、IPC 的基础知
  3. Android(安卓)Dagger2 初学笔记
  4. Android菜单Menu的创建
  5. Android两种序列化方式详解(一):Serializable
  6. Android(安卓)使用Parcelable序列化对象
  7. [置顶] 我的Android进阶之旅------>Android实现数据存储技术
  8. Android(安卓)SQLite总结[转载]
  9. Android(安卓)Service的启动流程源码分析(8.0)

随机推荐

  1. 实现Android屏幕控制Androidscreencast应
  2. Android支持的媒体格式
  3. Android(安卓)with the NDK_001
  4. Android(安卓)Studio 2.0 beta 7 发布
  5. Android(安卓)SDK Manager国内下载缓慢的
  6. Android(安卓)Editable
  7. org.opencv.android.JavaCameraView 摄像
  8. android音乐播放器Service的生命周期分析
  9. Android(安卓)隐式调用 intent
  10. 如何在win7下安装和配置Android(安卓)Stu