Phonegap - Android当软键盘可见时,如何在全屏模式下调整布局

时间:2013-11-08 00:24:32

标签: android cordova phonegap-build

我正在为三星Galaxy Tab 3开发一个phonegap应用程序。当此应用程序处于全屏模式时,软键盘会隐藏文本输入字段,并且无法滚动页面以查看内容。我怎么能解决这个问题?

2 个答案:

答案 0 :(得分:16)

在这个网站上花了一天时间尝试几乎所有可能的解决方案后,对我来说没有任何效果。最后,我能够根据以下两个提出的解决方案找到解决方法:

https://stackoverflow.com/a/19494006/1435991

此链接显示了解决Android应用程序问题的解决方法;但是我没有任何在android工作的经验,所以问题是:如何在Phonepap项目中包含这种代码的和平?。

https://stackoverflow.com/a/18610405

此链接特别建议使用phonegap的解决方案,这对我来说不起作用,但更重要的是如何在phonegap项目中添加自定义Android代码。

<强>解

1-在您的phonegap项目中创建以下类(如第一个链接中所示):



    package com.test.android;

    import android.app.Activity;
    import android.graphics.Rect;
    import android.view.View;
    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 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);
        }

}


这个类可以放在你项目的这个位置:(我无法在这个论坛中加载图像,我需要至少10个声望)。在此网址中找到图片示例:

check for more clarity

2-每次创建phonegap项目时,都会得到一个名为your_project_name.java的类。在我的例子中,它是test1.java。编辑类并在方法onCreate中添加以下句子:


    AndroidBug5497Workaround.assistActivity(this);

 

您的代码应如下所示:


    public class test1 extends DroidGap
    {
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            // Set by  in config.xml
            super.loadUrl(Config.getStartUrl());
            //super.loadUrl("file:///android_asset/www/index.html")
            AndroidBug5497Workaround.assistActivity(this);
        }
    }
     

3-这解决了我的应用中的问题。

答案 1 :(得分:3)

我实施了Jorge的答案,它对我很有用!但屏幕以笨重的方式调整大小,我希望它能够更顺畅地调整大小。所以我查看了如何为此视图制作动画并遇到this

将两者结合在一起看起来像这样:

import android.animation.ValueAnimator;
import android.app.Activity;
import android.graphics.Rect;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.animation.DecelerateInterpolator;
import android.widget.FrameLayout;

public class AdjustInputHeight {

    // 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 AdjustInputHeight(activity);
    }

    private View mChildOfContent;
    private int usableHeightPrevious;
    private ValueAnimator animateCollapseView = null;
    private ValueAnimator animateExpandView = null;
    private boolean keyboardIsUp = false;
    DecelerateInterpolator sDecelerator = new DecelerateInterpolator();
    private FrameLayout.LayoutParams frameLayoutParams;

    private AdjustInputHeight(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;

            //check if the view got smaller (because keyboard is shown) and is not already up.
            if (heightDifference > (usableHeightSansKeyboard/4) && (!this.keyboardIsUp)) {

                // we need to create the collapse animator the only the first time we rise the keyboard
                if (this.animateCollapseView == null) {
                    this.animateCollapseView = ValueAnimator.ofInt(usableHeightSansKeyboard, (usableHeightSansKeyboard-heightDifference));
                    this.animateCollapseView.setDuration(500);
                    this.animateCollapseView.setInterpolator(sDecelerator);
                    this.animateCollapseView.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                        public void onAnimationUpdate(ValueAnimator animation) {
                            Integer value = (Integer) animation.getAnimatedValue();
                            frameLayoutParams.height = value.intValue();
                            mChildOfContent.requestLayout();
                        }
                    });
                }

                this.animateCollapseView.start();
                // keyboard probably just became visible        
                this.keyboardIsUp = true;

            //lower the keyboard only if it is up.  
            } else if (this.keyboardIsUp) {

                // we need to create the expand animator the only the first time we lower the keyboard
                if (this.animateExpandView == null) {   
                    this.animateExpandView = ValueAnimator.ofInt((usableHeightSansKeyboard-heightDifference), usableHeightSansKeyboard);
                    this.animateExpandView.setDuration(200);
                    this.animateExpandView.setInterpolator(sDecelerator);
                    this.animateExpandView.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                        public void onAnimationUpdate(ValueAnimator animation) {
                            Integer value = (Integer) animation.getAnimatedValue();
                            frameLayoutParams.height = value.intValue();
                            mChildOfContent.requestLayout();
                        }
                    });
                }

                this.animateExpandView.start();
                // keyboard probably just became hidden
                this.keyboardIsUp = false;
            }

            usableHeightPrevious = usableHeightNow;
        }
    }

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

}

当然,您可以根据自己的需要更改动画持续时间和插值器。