2013-07-16 20 views
7

Ho bisogno di disabilitare la softkey aperta nella mia WebView e in tutti gli edittexts in WebView (non accedo a questo perché è in WebView).Come disabilitare il softkey in WebView

Provo ad usare 'android: windowSoftInputMode = "stateAlwaysHidden"' nel mio file Manifest, ma popup della tastiera su quando clicco nel campo modificabile.

Qual è la soluzione giusta per disabilitare il softkeyboard in WebView?

EDIT:

trovo soluzione (grazie @ g00dy in questo post e @Kachi in post https://stackoverflow.com/a/9108219/1665964) una stretta tastiera dopo che è aperto:

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

Questo codice anche vicino messaggio di sistema " Modifica testo/Metodo di inserimento "quando l'utente pigia sul campo di inserimento.

Ma! Questo codice chiude la tastiera solo dopo l'apertura. La tastiera rimane visibile per alcuni millisecondi e l'utente (utente veloce) può premere qualsiasi tasto sulla tastiera. Questa non è la situazione migliore.

Forse esiste il modo migliore per disabilitare al 100% la tastiera senza aprirla?

risposta

1

Per lo stato nascosto tastiera:

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

Per disattivare il EditTexts:

Set android:editable="false" nel layout di loro EditText.setFocusable(false) nell'attività.

EDIT:

Per rilevare l'evento click nella WebView uso questo:

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

non ho accesso a cambiare gli attributi del EditText perché ho solo il mio WebView. –

+0

Quindi non si dispone dei diritti per modificare gli attributi di 'EditText', che si trova all'interno di un' WebView', a cui si ha accesso - come mai? Qual è il contenuto di 'WebView'? Stai visualizzando una pagina web sotto il tuo controllo o un'altra pagina web casuale? – g00dy

+0

Dovresti invece provare a rilevare il clic, modifico il mio post. – g00dy

5

Questa risposta ha funzionato per me (fornita da Android Weblineindia): https://stackoverflow.com/a/29409478/4813198

Add seguente codice nel layout principale (principale) nel file layout.xml:

> 
> android:descendantFocusability="blocksDescendants" 
> 

e impostare le seguenti proprietà nel WebView:

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

Una sorta di soluzione di trucco che mi è venuta in mente, ma ancora fa ciò che è necessario - nasconde la tastiera in modo utente non sarà leggi:

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

} 

Sentiti libero di giocare con i ritardi per ridurre al minimo il sovraccarico a causa della chiamata rapida di un divertimento ction durante il periodo predefinito. Credo che i valori ottimali possono variare a seconda del dispositivo.

-1

Prova una soluzione del genere, è abbastanza semplice:

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/"); 
Problemi correlati