Android平台4.2.2与Cordova2.2.0结合使用后出现软键盘弹出后,覆盖输入域问题,在Android的4.0.3中测试没有这样的问题,设置AndroidManifest.xml中的activity的android:windowSoftInputMode="adjustPan"不起作用,通过google发现者可能是android的一个bug或者是cordova的bug。。。

借鉴了网上的一种写法

代码如下:

package com.siro.androidwebviewos.utils;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Rect;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;

import com.siro.androidwebviewos.action.OpenNewActivity;
@SuppressLint("NewApi")
public class AndroidBug5497Workaround {

public static void assistActivity (OpenNewActivity activity) {
new AndroidBug5497Workaround(activity);
}

private View rootView ;
private int mLastHeightDifferece ;

private AndroidBug5497Workaround(Activity activity) {


rootView = activity.findViewById(android.R.id.content);

rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
checkHeightDifference();
}
});
}

private void checkHeightDifference(){
// get screen frame rectangle
Rect r = new Rect();
rootView.getWindowVisibleDisplayFrame(r);
int screenHeight = rootView.getRootView().getHeight();
int heightDifference = screenHeight - (r.bottom - r.top);
if (heightDifference > screenHeight/3 && heightDifference != mLastHeightDifferece) {
rootView.getLayoutParams().height = screenHeight - heightDifference;
rootView.requestLayout();
mLastHeightDifferece = heightDifference;
} else if (heightDifference != mLastHeightDifferece) {
rootView.getLayoutParams().height = screenHeight;
rootView.requestLayout();
mLastHeightDifferece = heightDifference;
}
}

}

activity中调用:

在loadUrl后调用

AndroidBug5497Workaround.assistActivity(this);

效果不理想,虽然有屏幕上推的效果,但是,输入域还是被覆盖。

经过同事提醒改变了一下写法,效果还可以。

public class OpenNewActivity  extends DroidGap implements OnTouchListener 实现OnTouchListener 

重写onTouch方法,主要获取getRawY

@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
eventX = event.getRawX() ;
eventY = event.getRawY() ;
}
return false;
}

oncreate方法的loadUrl之后增加对rootView的监听:

   rootView = this.findViewById(android.R.id.content);

    rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

            public void onGlobalLayout() {

            checkHeightDifference();

            }

        });

通过当前屏幕高度,弹出后的软键盘高度及所点击输入域的RawY。计算rootView的位移量,并且requestLayout();

 @SuppressLint("NewApi")

private void checkHeightDifference(){

        Rect r = new Rect();

        rootView.getWindowVisibleDisplayFrame(r);

        int screenHeight = rootView.getRootView().getHeight();

        int heightDifference = screenHeight - (r.bottom - r.top);

        if (heightDifference > screenHeight/3 && heightDifference != mLastHeightDifferece) {

        rootView.setTranslationY(- (heightDifference - ( screenHeight - eventY)+20));

            rootView.requestLayout();

            mLastHeightDifferece = heightDifference;

        } else if (heightDifference != mLastHeightDifferece) {

        rootView.setTranslationY(heightDifference);

            rootView.requestLayout();

            mLastHeightDifferece = heightDifference;

        }

}

onTouch

posted on 2014-03-05 15:10  mu大仙  阅读(3847)  评论(0编辑  收藏  举报