【问题标题】:Phonegap - Android How to adjust layout in Full Screen Mode when softkeyboard is visiblePhonegap - Android 如何在软键盘可见时在全屏模式下调整布局
【发布时间】:2023-03-19 08:45:01
【问题描述】:

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

【问题讨论】:

  • 我没有看到任何问题...
  • 乔治,你是对的。刚刚添加了问题。我必须等待 8 小时才能添加我自己的 anwser

标签: android cordova phonegap-build


【解决方案1】:

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

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 个声望)。在此 url 中找到图像示例:

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

AndroidBug5497Workaround.assistActivity(this);

您的代码应如下所示:

公共类 test1 扩展了 DroidGap { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 在 config.xml 中设置 super.loadUrl(Config.getStartUrl()); //super.loadUrl("file:///android_asset/www/index.html") AndroidBug5497Workaround.assistActivity(this); } }

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

【讨论】:

  • 谢谢,适用于带有 Galaxy S3 的 Cordova 3 应用程序
  • 可以确认此代码在 2 年后在我运行 4.4.4 的 OnePlus 上仍然有效 - 非常感谢!
  • 我应该在 Cordova 6.0.0 上使用此修复程序,但它仍然像魅力一样工作。 Cordova 会修复它还是我错过了什么?
【解决方案2】:

我实施了 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);
    }

}

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

【讨论】:

  • 好主意,但如果您必须在同一视图中输入文本,则不起作用
猜你喜欢
  • 2011-11-17
  • 1970-01-01
  • 2011-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多