Android 键盘隐藏WebView中的输入字段
我有一个布局:Android 键盘隐藏WebView中的输入字段,android,webview,android-softkeyboard,Android,Webview,Android Softkeyboard,我有一个布局: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".StreamActivity"
android:orientation="vertical">
<FrameLayout
android:id="@+id/streamLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<SurfaceView
android:id="@+id/surface"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start">
</SurfaceView>
</FrameLayout>
<WebView
android:id="@+id/web_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="start"/>
视频播放器的框架布局。WebView是一种聊天工具。
当我开始在聊天中输入文本时,键盘会隐藏输入字段
我试着试用android:WindowsOfInputMode,但它对我不起作用。聊天的问题是,它写在角度上,调整WebView的大小不会改变聊天的大小,必须像WebView.loadUrl(“javascript:$('section.content window:first')).css('height','“+finalSize+'px');”)一样手动完成
我有一个当键盘显示和隐藏时触发的侦听器。
我想在显示键盘时手动更改聊天的大小,但效果不好,因为当我更改聊天的大小时,它会失去焦点并隐藏键盘
我还想把活动的内容放在ScrollView中。如果覆盖OnTouchListener,这将锁定用户的滚动,但我仍然能够从代码中滚动。所以,我想,如果显示键盘,然后滚动活动的内容,使其在整个聊天中可见。我认为这会起作用,但并不好,因为它会锁定WebView中的滚动
建议如何解决键盘和聊天的问题?我找到了一个解决方案:
<ScrollView ...
<LinearLayout ...
...
<com.package.TouchyWebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/webView"/>
...
</LinearLayout>
</ScrollView>
及
scrollView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
为了滚动,我使用这样的代码
final View activityRootView = findViewById(R.id.main_layout);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
boolean isOpen = false;
@Override
public void onGlobalLayout() {
if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
if (!isOpen) {
scrollView.scrollBy(0, heightDiff);
isOpen = true;
}
} else {
if (isOpen) {
scrollView.scrollBy(0, -heightDiff);
isOpen = false;
}
}
}
});
嘿,你能告诉我们你从哪里得到的高度差吗?谢谢
final View activityRootView = findViewById(R.id.main_layout);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
boolean isOpen = false;
@Override
public void onGlobalLayout() {
if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
if (!isOpen) {
scrollView.scrollBy(0, heightDiff);
isOpen = true;
}
} else {
if (isOpen) {
scrollView.scrollBy(0, -heightDiff);
isOpen = false;
}
}
}
});