Android在全屏状态下键盘覆盖输入框问题

        Android中有个Bug,在设置getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);为全屏状态下点击输入框显示键盘时,界面并不会重新渲染调整位置,android:windowSoftInputMode="adjustResize"在FullScreen下没有作用。

       找了很久才找到解决方法,该方法是在Activity onCreate时通过ViewTreeObserver注册GlobalLayoutListener监听,当全局布局改变时会触发该监听。下面是代码部分:

package com.maituo.sdk.util;

import android.app.Activity;
import android.graphics.Rect;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;

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 mContent;
    private int usableHeightPrevious;
    private LayoutParams layoutParams;
    public static void assistView(View v){
    	new AndroidBug5497Workaround(v);
    }
    
    public static void assistActivity (Activity activity) {
        new AndroidBug5497Workaround(activity);
    }
    
    private AndroidBug5497Workaround(Activity activity){
    	 FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);
    	 mContent = content.getChildAt(0);
    	 addGlobalLayoutListener(mContent);
       
    }
    //有时通过Activity获取view并不能满足,所以我加了直接传view的一个构造方法满足用到Fragment的情况
    private AndroidBug5497Workaround(View v) {
        //FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);
       // mChildOfContent = content.getChildAt(0);
    	addGlobalLayoutListener(v);
    	
    }
    
    private void addGlobalLayoutListener(View v){
    	mContent = v;
    	mContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            public void onGlobalLayout() {
                possiblyResizeChildOfContent();
            }
        });
    //  LayoutParams放在这里有可能得到的为空
    //  LayoutParams =  mContent.getLayoutParams();
    }

    private void possiblyResizeChildOfContent() {
        int usableHeightNow = computeUsableHeight();
        if (usableHeightNow != usableHeightPrevious) {
        	layoutParams = mContent.getLayoutParams();
            int usableHeightSansKeyboard = mContent.getRootView().getHeight();
            int heightDifference = usableHeightSansKeyboard - usableHeightNow;
            if (heightDifference > (usableHeightSansKeyboard/4)) {
                // keyboard probably just became visible 
            	layoutParams.height = usableHeightSansKeyboard - heightDifference;
            } else {
                // keyboard probably just became hidden
            	layoutParams.height = usableHeightSansKeyboard;
            }
            mContent.requestLayout();
            usableHeightPrevious = usableHeightNow;
        }
    }

    private int computeUsableHeight() {
        Rect r = new Rect();
        mContent.getWindowVisibleDisplayFrame(r);
        return (r.bottom - r.top);
    }

}

 

只要调用AndroidBug5497Workaround.assistActivity(activity)或AndroidBug5497Workaround.assistView(view)就可以了


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值