2015-10-13 126 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; 
      } 
     } 
    } 
});