p***@yeah.net
p***@yeah.net
  • 发布:2017-01-18 09:52
  • 更新:2017-01-18 09:52
  • 阅读:7642

解决安卓全屏“FLAG_FULLSCREEN”状态下“adjustResize”失效,全屏状态下WebView的输入框被软键盘挡住的问题

分类:HTML5+

  目前,5+SDK在全屏模式下问题比较多,比如webview的show和hide方法会概率性失效(调用后没反应)、软键盘挡住当前具有焦点的文本输入框等问题。
  本篇文章的意图,希望官方看到后可以引入到5+SDK中,以便解决全屏模式下,软键盘盖住文本框的问题。

  沿着这个问题的线索,可以追溯到:http://code.google.com/p/android/issues/detail?id=5497 ,安卓官方问题回馈帖,这个问题的代号为“5497” ,就这个问题帖的回复来看,该问题困惑了许多人数年之久,问题发布日期“Dec 16, 2009”,现在我在安卓4.4.2环境运行,这个问题依旧存在。在此推荐这其中的一个解决方法,来自:stackoverflow.com,实测有效。


[java] view plaincopy  
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);  
    }  
}  

在Activity/Fragment的onCreate()/onCreateView()里调用AndroidBug5497Workaround.assistActivity(Activity);代码搬运,希望能够帮助到各位。

另见:
必须在setContentView()后面添加AndroidBug5497Workaround.assistActivity(this);

Based on yghm is workaround, I coded up a convenience class that allows me to solve the problem with a one-liner (after adding the new class to my source code of course). The one-liner is:  
     AndroidBug5497Workaround.assistActivity(this);  
And the implementation class is:  
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);  
    }  
}  
Hope this helps someone.  
0 关注 分享

要回复文章请先登录注册

evans

evans

回复 p***@yeah.net :
所以用全屏模式都会有这个问题?
2017-05-05 13:35
r***@163.com

r***@163.com

官方赶紧关注吧,沉浸式和全屏bug多的简直令人发指。
2017-02-27 15:17
p***@yeah.net

p***@yeah.net (作者)

回复 gaus :
Android暂时只能这样了,IOS不受影响。
2017-02-23 16:02
gaus

gaus

回复 p***@yeah.net :
看来只能先不用全屏了,多谢!
2017-02-23 14:39
p***@yeah.net

p***@yeah.net (作者)

回复 gaus :
这个是SDK的事情,上层做不了的。
2017-02-23 13:55
gaus

gaus

兄弟,非sdk模式下,有无解决办法?
2017-02-21 09:35