如何在 WebView 中禁用软键盘 [英] How disable softkeyboard in WebView

查看:15
本文介绍了如何在 WebView 中禁用软键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的 WebView 和 WebView 中的所有编辑文本中禁用打开软键盘(我无法访问它,因为它在 WebView 中).

我尝试在我的 Manifest 文件中使用 'android:windowSoftInputMode="stateAlwaysHidden"',但当我点击可编辑字段时键盘弹出.

在 WebView 中禁用软键盘的正确解决方案是什么?

我找到了解决方案(感谢这篇文章中的@g00dy 和 https://stackoverflow.com/a/9108219/1665964) 打开后关闭键盘:

公共类ActivityBrowser扩展Activity{私有静态 WebView webviewHTML;私有静态视图 viewRootHTML;私有静态 int iViewRootHTMLHeightDifferent;公共静态上下文上下文浏览器;{上下文浏览器 = 这个;}公共类 webViewClient 扩展了 WebViewClient{@覆盖public void onPageStarted(WebView 视图、字符串 URL、位图图标){if( view == webviewHTML) super.onPageStarted( view, url, favicon);}@覆盖public void onPageFinished(WebView view, String url){if( view == webviewHTML) super.onPageFinished( view, url);}@覆盖public boolean shouldOverrideUrlLoading(WebView view, String url){if(view == webviewHTML) view.loadUrl( url);返回假;//返回 super.shouldOverrideUrlLoading( view, url);}@覆盖public void onReceivedError(WebView view, int errorCode, String description, String failedUrl){if( view == webviewHTML) ApplicationLeta.fPopup( getString( R.string.sPopupErrorSiteOpen) + " : " + description);//ActivityBrowser.this.finish();}public void onReceivedSslError(WebView 视图,SslErrorHandler 处理程序,SslError 错误){if( view == webviewHTML) handler.proceed();}}@覆盖公共布尔 dispatchTouchEvent(MotionEvent 运动事件){super.dispatchTouchEvent(motionEvent);if(motionEvent.getAction() == MotionEvent.ACTION_MOVE) 返回真;if(motionEvent.getAction() == MotionEvent.ACTION_UP){//做一点事}if(motionEvent.getAction() == MotionEvent.ACTION_UP){//做一点事}返回假;}@覆盖公共无效 onBackPressed(){}@覆盖public void onWindowFocusChanged(boolean eFocus){super.onWindowFocusChanged(eFocus);if(eFocus == false){fKeyboardClose();新线程(新可运行(){@覆盖公共无效运行(){尝试{Instrumentation inst = new Instrumentation();inst.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);}捕获(异常 e){}}} ).开始();}}私有无效 fKeyboardClose(){InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(Activity.INPUT_METHOD_SERVICE);inputMethodManager.hideSoftInputFromWindow( getCurrentFocus().getWindowToken(), 0);}public OnGlobalLayoutListener onGlobalLayoutListener = new OnGlobalLayoutListener(){@覆盖公共无效 onGlobalLayout(){矩形矩形 = 新矩形();viewRootHTML.getWindowVisibleDisplayFrame(rect);iViewRootHTMLHeightDifferent = viewRootHTML.getRootView().getHeight() - (rect.bottom - rect.top);if(iViewRootHTMLHeightDifferent > 50) fKeyboardClose();}};@SuppressWarnings(弃用")@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% 禁用键盘的最佳方法?

解决方案

这个答案对我有用(由 Android Weblineindia 提供):https://stackoverflow.com/a/29409478/4813198

<块引用>

在 layout.xml 的主(父)布局中添加以下代码:

>>android:descendantFocusability="blocksDescendants">

<块引用>

并在您的 webview 中设置以下属性:

>机器人:焦点=假">android:focusableInTouchMode="true"

I need disable open softkeyboard in my WebView and in all edittexts in WebView (I do not access to thay because its is in WebView).

I try use 'android:windowSoftInputMode="stateAlwaysHidden"' in my Manifest file, but keyboard popup on when I click in editable field.

What is right solution for disable softkeyboard in WebView?

EDIT:

I find solution (thanks @g00dy in this post and @Kachi in post https://stackoverflow.com/a/9108219/1665964) for close keyboard after it open:

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

This code also close system message "Edit text / Input method" when user longpress on input field.

But! This code close keyboard only after it open. Keyboard stay visible a few miliseconds and user (fast user) can press any key on keyboard. This is not best situation.

Maybe exist best way to 100% disable keyboard without open it?

解决方案

This answer worked for me (provided by Android Weblineindia): https://stackoverflow.com/a/29409478/4813198

Add following code in main (parent) layout in your layout.xml:

>
>    android:descendantFocusability="blocksDescendants"
>

and set following properties in your webview:

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

这篇关于如何在 WebView 中禁用软键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆