2015-04-02 60 views
4

我知道這是一個常見問題,但我無法在Google和Google上找到有效的解決方案。在WebView中禁用SoftKeyboard

我裏面有一個簡單的DatePicker組件在我的JSF頁面Android應用程序的WebView:

<p:calendar value="#{patientHealthDataView.dateFrom}" id="popupButtonDateFrom" showOn="button" class="tableCalendarText"/> 

,顯示與日曆上彈出。

由於這個命令,我設法使彈出的作品:

WebView view=(WebView)findViewById(R.id.statistics); 
view.getSettings().setJavaScriptEnabled(true); 

Unfortunally,當我在輸入文本或單擊按鈕來顯示日曆的彈出窗口中,將重點轉移到輸入文本和Android秀其鍵盤隱藏日曆。

我試過幾種解決方案。最後一個是這樣的:

view.setOnTouchListener(new View.OnTouchListener() { 

     public boolean onTouch(View v, MotionEvent event) { 
      hideSoftKeyboard(v); 
      return false; 
     } 
    }); 

public void hideSoftKeyboard(View v) { 
     Activity activity = (Activity) v.getContext(); 
     InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE); 
     inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0); 
    } 

此代碼,由Android工作室的建議,提高對空指針異常:

activity.getCurrentFocus().getWindowToken() 

其他的解決方案表示插入清單中一個特殊的命令,但我想只在WebView中阻止鍵盤。

有什麼建議嗎?

+1

你試過'inputMethodManager.hideSoftInputFromWindow(webView.getWindowToken(),0);''那裏是webView'你'的WebView視圖=(的WebView)findViewById( R.id.statistics);'? – NSimon 2015-04-02 08:51:32

+0

感謝您的回答。我剛剛嘗試過。不幸的是,這種解決方案不起作用。 – MrMime 2015-04-02 09:13:06

+0

看起來最好的「黑客」,但是要知道什麼時候部署了鍵盤,並從那裏隱藏它​​。解決方案可以在這個問題上找到:http://stackoverflow.com/q/17672116/4706693 – NSimon 2015-04-02 09:18:58

回答

10

加入您的layout.xml按照主(父)佈局代碼

android:descendantFocusability="blocksDescendants" 

希望它會工作。

,並設置按照您的WebView屬性

android:focusable="false" 
android:focusableInTouchMode="true" 
+0

我的WebView partent標籤是一個LinearLayout。我試過使用你的代碼如下: android:descendantFocusability =「beforeDescendants」 android:focusableInTouchMode =「true」 但它不起作用。 我也嘗試過在focusableInTouchMode中使用「false」,但它是一樣的。鍵盤仍然顯示。 – MrMime 2015-04-02 10:15:05

+0

請檢查我編輯的答案。 – 2015-04-02 10:27:31

+0

它的作品!萬分感謝!!! – MrMime 2015-04-02 12:25:15