2013-07-16 110 views
7

我需要禁用WebView中的打開軟鍵盤和WebView中的所有編輯文本(我不訪問泰語,因爲它在WebView中)。如何在WebView中禁用軟鍵盤

我嘗試在我的Manifest文件中使用'android:windowSoftInputMode =「stateAlwaysHidden」',但當我在可編輯字段中單擊時鍵盤彈出。

在WebView中禁用軟鍵盤的正確解決方案是什麼?

編輯:

我找到的解決方案(在這個崗位和@Kachi在後https://stackoverflow.com/a/9108219/1665964感謝@ g00dy)爲關閉虛擬鍵盤之後它打開:

public class ActivityBrowser extends Activity 
{ 
    private static WebView  webviewHTML; 
    private static View   viewRootHTML; 
    private static int   iViewRootHTMLHeightDifferent; 
    public static Context  contextBrowser; 

    { 
     contextBrowser = this; 
    } 


    public class webViewClient extends WebViewClient 
    { 
     @Override 
     public void onPageStarted(WebView view, String url, Bitmap favicon) 
     { 
     if(view == webviewHTML) super.onPageStarted(view, url, favicon); 
     } 

     @Override 
     public void onPageFinished(WebView view, String url) 
     { 
     if(view == webviewHTML) super.onPageFinished(view, url); 
     } 

     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) 
     { 
     if(view == webviewHTML) view.loadUrl(url); 
     return false; 
     // return super.shouldOverrideUrlLoading(view, url); 
     } 

     @Override 
     public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) 
     { 
     if(view == webviewHTML) ApplicationLeta.fPopup(getString(R.string.sPopupErrorSiteOpen) + " : " + description); 
     // ActivityBrowser.this.finish(); 
     } 

     public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) 
     { 
     if(view == webviewHTML) handler.proceed(); 
     } 
    } 


    @Override 
    public boolean dispatchTouchEvent(MotionEvent motionEvent) 
    { 
     super.dispatchTouchEvent(motionEvent); 

     if(motionEvent.getAction() == MotionEvent.ACTION_MOVE) return true; 

     if(motionEvent.getAction() == MotionEvent.ACTION_UP) 
     { 
      // do something 
     } 

     if(motionEvent.getAction() == MotionEvent.ACTION_UP) 
     { 
      // do something 
     } 
     return false; 
    } 


    @Override 
    public void onBackPressed() 
    { 
    } 


    @Override 
    public void onWindowFocusChanged(boolean eFocus) 
    { 
     super.onWindowFocusChanged(eFocus); 
     if(eFocus == false) 
     { 
      fKeyboardClose(); 

      new Thread(new Runnable() 
      { 
      @Override 
      public void run() 
       { 
       try 
        { 
        Instrumentation inst = new Instrumentation(); 
        inst.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK); 
        } 
       catch(Exception e) {} 
       } 
      }).start(); 
     } 
    } 


    private void fKeyboardClose() 
    { 
     InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(Activity.INPUT_METHOD_SERVICE); 
     inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0); 
    } 


    public OnGlobalLayoutListener onGlobalLayoutListener = new OnGlobalLayoutListener() 
    { 
     @Override 
     public void onGlobalLayout() 
     { 
     Rect rect = new Rect(); 
     viewRootHTML.getWindowVisibleDisplayFrame(rect); 
     iViewRootHTMLHeightDifferent = viewRootHTML.getRootView().getHeight() - (rect.bottom - rect.top); 
     if(iViewRootHTMLHeightDifferent > 50) fKeyboardClose(); 
     } 
    }; 

    @SuppressWarnings("deprecation") 
    @SuppressLint("SetJavaScriptEnabled") 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.browser); 

     if(savedInstanceState == null) 
     { 
      viewRootHTML = findViewById(R.id.linearLayoutHTML); 
      viewRootHTML.getViewTreeObserver().addOnGlobalLayoutListener(onGlobalLayoutListener); 

      webviewHTML = (WebView) findViewById(R.id.webviewHTML); 
      WebSettings webSettings = webviewHTML.getSettings(); 
      webSettings.setJavaScriptEnabled(true); 
      webSettings.setJavaScriptCanOpenWindowsAutomatically(true); 
      webviewHTML.setWebViewClient(new wiewClient()); 
      webviewHTML.loadUrl(ApplicationLeta.sAppInterviewURL); 
     } 
    } 
} 

這段代碼也接近系統消息「編輯文本/輸入法「當用戶長按輸入字段。

但是!此代碼僅在打開後關閉鍵盤。鍵盤保持可見幾毫秒,並且用戶(快速用戶)可以按下鍵盤上的任何鍵。這不是最好的情況。

也許存在最好的方式100%禁用鍵盤而不打開它?

回答

1

對於鍵盤隱藏狀態:在他們EditText.setFocusable(false)的活動中的佈局

android:editable="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); 
     } 

禁用EditTexts

編輯:

爲了檢測WebView使用click事件這樣的:

mWebView.setOnTouchListener(new View.OnTouchListener() { 
     public boolean onTouch(View v, MotionEvent event) { 
      // The code of the hiding goest here, just call hideSoftKeyboard(View v); 
      return false; 
      }  
     }); 
+1

我沒有獲得改變的EditText的屬性,因爲我只有我的WebView。 –

+0

因此,您無權更改「EditText」的屬性,該屬性位於「WebView」內部,您可以訪問該內容 - 該怎麼辦? 「WebView」的內容是什麼?您是否在可控制或其他隨機網頁下可視化網頁? – g00dy

+0

你應該嘗試檢測點擊,我會編輯我的帖子。 – g00dy

5

這個答案爲我工作(由Android Weblineindia提供):https://stackoverflow.com/a/29409478/4813198

添加以下代碼在layout.xml中的main(父級)佈局中:

> 
> android:descendantFocusability="blocksDescendants" 
> 

,並設置按照您的WebView屬性:

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

A類黑客攻擊的解決方案,來我的腦海裏,但仍然沒有需要什麼 - 隱藏鍵盤,使用戶不會看到它:

public class WebViewEx extends WebView { 

    private Handler mHandler; 

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

    private void ensureKeyboard() { 
     if(mHandler == null){ 
      mHandler = new Handler() { 
       @Override 
       public void handleMessage(Message msg) { 
        super.handleMessage(msg); 
        closeKeyboard(); 
        sendEmptyMessageDelayed(0, 10); 
       } 
      }; 
     } 
     mHandler.removeCallbacksAndMessages(null); 
     mHandler.sendEmptyMessage(0); 
     mHandler.postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       mHandler.removeCallbacksAndMessages(null); 
      } 
     }, 300); 
    } 

    private void closeKeyboard() { 
     InputMethodManager inputMethodManager = (InputMethodManager) getContext().getSystemService(Activity.INPUT_METHOD_SERVICE); 
     inputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0); 
    } 

} 

隨意玩延遲,以儘量減少開銷,因爲快速通話的樂趣在預定義的時間段內。我相信最佳值可能會因設備而異。

-1

嘗試這樣的解決方案,這是很簡單的:

final WebView webView = (WebView) findViewById(R.id.webView); 
webView.getSettings().setJavaScriptEnabled(true); 

final String js = "javascript: var allInputs = document.getElementsByTagName('input'); for (var i = 0, len = allInputs.length; i < len; ++i) { allInputs[i].readOnly = true;}"; 
webView.setWebViewClient(new WebViewClient(){ 
    @Override 
    public void onPageFinished(WebView view, String url) { 
     if (Build.VERSION.SDK_INT >= 19) { 
      view.evaluateJavascript(js, new ValueCallback<String>() { 
      @Override 
      public void onReceiveValue(String s) { } 
     }); 
     } else { 
      view.loadUrl(js); 
     } 
    } 
}); 
webView.loadUrl("https://www.google.de/");