我正在为三星 Galaxy Tab 3 开发 phonegap 应用程序。当此应用程序处于全屏模式时,软键盘会隐藏文本输入字段,并且无法滚动页面以查看内容。我该如何解决这个问题?
2 回答
在花了一天时间尝试了这个网站上几乎所有可能的解决方案之后,对我来说没有任何效果。最后,我能够根据以下两个建议的解决方案找到解决方法:
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);
您的代码应如下所示:
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-这解决了我的应用程序中的问题。
我实施了 Jorge 的答案,它对我很有用!但是屏幕会以一种笨重的方式自行调整大小,我希望它能够更顺畅地调整大小。所以我查找了如何为这个视图设置动画并遇到了这个。
将两者结合在一起看起来像这样:
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);
}
}
当然,您可以根据自己的需要更改动画持续时间和插值器。