gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 01:35:12 26 4
gpt4 key购买 nike

在我的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com