• Android Cordova 对于软键盘弹出后覆盖输入域的解决


      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

  • 相关阅读:
    python 对xls写入信息
    Python 字符串前面加u,r,b,f的含义
    inner join 与 left join 之间的区别
    时间戳转换成日期展示的方法 且 搜索范围
    Python与C/C++相互调用(python2 调c++那个试了ok)
    爆库记录(X-Forwarded-For注入漏洞实战 记录)
    笔记
    墨者学习安全测试的网站(看起来很不错的样子 有空看看)
    sqlmap开源 测试sql注入的工具 各种参考链接
    菜鸟浅谈——web安全测试(这篇不错有空看看)
  • 原文地址:https://www.cnblogs.com/bigcelestial/p/3582530.html
Copyright © 2020-2023  润新知