2015-10-13 127 views
3

我有一个布局:键盘隐藏输入字段的WebView

<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"/> 

的FrameLayout视频播放器。 WebView是一个聊天。 当我开始在聊天中键入文本时,键盘会隐藏输入字段。

我尝试过试用android:windowSoftInputMode,但它不适用于我。聊天的问题,这是写在Angular和调整WebView的大小不会改变聊天的大小,它必须手动完成,如webView.loadUrl(“javascript:$('section.content-window:first')。css 'height',''+ finalSize +“px');”);

我有一个监听器,当键盘显示和隐藏时触发。 我想在显示键盘时手动更改聊天的大小,但不能很好地工作,因为当我改变聊天的大小时,它会失去焦点并隐藏键盘。

我还认为把活动的内容放在ScrollView中。如果覆盖OnTouchListener,这将锁定用户的滚动,但我仍然可以从代码滚动。所以,我想如果显示键盘然后滚动活动的内容,这样就可以看到整个聊天。我认为这会起作用,但不好,因为它会锁定WebView中的滚动。

建议如何解决键盘和聊天问题?

回答

0

我找到了一个解决方案:

<ScrollView ... 
     <LinearLayout ... 
      ... 
      <com.package.TouchyWebView 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:id="@+id/webView"/> 
      ... 
     </LinearLayout> 
</ScrollView> 

,做这样的

package com.mypackage.common.custom.android.widgets 

public class TouchyWebView extends WebView { 

    public TouchyWebView(Context context) { 
     super(context); 
    } 

    public TouchyWebView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public TouchyWebView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event){ 
     requestDisallowInterceptTouchEvent(true); 
     return super.onTouchEvent(event); 
    }   
} 

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; 
      } 
     } 
    } 
});