android - 键盘隐藏了 Android WebView 上的输入字段

标签 android webview android-webview android-softkeyboard

在我的 Android 应用程序中,我有一个 Activity 的布局几乎是 WebView 元素。在此 WebView 中,我正在加载在线表单供用户填写。但是,底部输入字段(文本框)保留在软键盘后面,用户无法填写它们。

对于这种情况有哪些可能的解决方案?

这是我的代码:

AndroidManifest.xml(仅此布局部分)

<activity
        android:name=".home.SurveyActivity"
        android:configChanges="orientation"
        android:icon="@drawable/logo_white"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="adjustResize">

    </activity>

布局文件:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/custom_header"
        style="@style/CustomHeader.Height"
        android:layout_alignParentTop="true"/>

    <WebView
        android:id="@+id/activity_portal_webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/activity_portal_bottom_layout"
        android:layout_below="@id/custom_header"
        android:scrollbars="none"/>

    <LinearLayout
        android:id="@id/activity_portal_bottom_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="vertical">

    </LinearLayout>

</RelativeLayout>

Activity 类:

public class SurveyActivity extends Activity {

    public static Intent makeIntent(Context context) {
        return new Intent(context, SurveyActivity.class);
    }

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Utils.updateLang();

        setContentView(R.layout.survey_activity_main);

        Utils.inflateCustomHeader(this);

        WebView webView = (WebView) findViewById(R.id.activity_portal_webview);

        webView.setWebChromeClient(new WebChromeClient());

        WebSettings webSettings = webView.getSettings();
        webSettings.setLoadsImagesAutomatically(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDefaultTextEncodingName("UTF-8");

        webView.loadUrl("WWW.EXAMPLE.COM");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Utils.updateLang();

        LinearLayout navigationBar = (LinearLayout) findViewById(R.id.activity_portal_bottom_layout);
        navigationBar.removeAllViews();
        navigationBar.addView(NavigationBarContract.makeNavigationBar(this, R.id.navigation_bar_home_button));
    }
}

最佳答案

我不确定这是否有帮助,但我的解决方案是向 webview 添加一些 javascript 来处理调整大小。

首先您需要两个 javascript 函数,我将我的调用为 noFocus,它将主体移回 0 位置。

function noFocus() {
    console.log('unfocus');
    Android.resetWindow(); // this removes the naigation and status bar via an interface
    $('body').css("margin-top", "0px");
 }

另一个叫 hasFocus 决定天气是否向上移动 body

function hasFocus(){
  var boxHeight = $(this).offset().top +60;
  var windowHei = $(window).height();
  var dTop      = boxHeight - (windowHei/2);
  // this logic may need to change depending on your implementation
  console.log('dtop' + dTop + ' as boxheight is ->' + boxHeight + 'windowHei ->' + windowHei )
if(boxHeight > (windowHei/2)) {
    $('body').css("margin-top", -dTop + "px");
}
this.addEventListener('blur', noFocus, false);
}

然后您需要遍历 DOM,添加以下输入监听器

var input = document.querySelectorAll('input');       
for (var i = 0; i < input.length; i++) {
    input[i].removeEventListener('focus', hasFocus, false)
    input[i].addEventListener('focus', hasFocus, false)
};

我的方法的唯一限制是,如果键盘被关闭,则 noFocus 不会被调用,直到用户触摸输入字段之外。

关于android - 键盘隐藏了 Android WebView 上的输入字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28654865/

相关文章:

javascript - 如果使用 loadUrl() 而不是 evaluateJavascript() 在 webView 中启动,则 JS 代码有效

java - native 浏览器和 Android 应用程序之间是否可以交互?

ios - 快速获取webview的滚动位置

javascript - 如何从 javascript 禁用 android ImageButton

javascript - 如何在 webview 内的 web 之间进行通信以响应 native 应用程序

android - 如何在 webview 中禁用与 html5 视频的交互或正确捕获它们的异常?

android - WebView Open External Links & target=blank Links in New Window like GMail 应用程序

android - Admob:每个 Activity 一个横幅还是所有 Activity 一个横幅?

c# - 在 Xamarin.Forms 中,在我将 ScrollView 添加到页面后,我所有的点击事件都停止工作。 ScrollView 是否拦截点击?

java - 在 Java (Android) 中检查 null 和 "emptyness"--> 崩溃