如何禁用softkeyboard中的WebView [英] How disable softkeyboard in WebView

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

问题描述

我需要禁用开softkeyboard在我的WebView,并在web视图所有edittexts(我不访问大公,因为它是在web视图)。

我尝试使用机器人:windowSoftInputMode =stateAlwaysHidden'。在我的清单文件,但是当我点击编辑字段键盘弹出

什么是web视图中禁用softkeyboard合适的解决方案?

编辑:

我觉得解决方案(感谢@ g00dy在这个岗位和@Kachi后 http://stackoverflow.com/a/9108219/ 1665964 )密切键盘之后它打开:

 公共类ActivityBrowser扩展活动
 {
   私有静态的WebView webviewHTML;
   私有静态视图viewRootHTML;
   私有静态诠释iViewRootHTMLHeightDifferent;
   公共静态语境contextBrowser;

    {
      contextBrowser =这一点;
    }


   公共类webViewClient扩展WebViewClient
    {
      @覆盖
      公共无效onPageStarted(web视图查看,字符串URL,位图图标)
       {
         如果(查看== webviewHTML)super.onPageStarted(查看,网址,网站图标);
       }

      @覆盖
      公共无效onPageFinished(web视图查看,字符串URL)
       {
         如果(查看== webviewHTML)super.onPageFinished(查看,网址);
       }

      @覆盖
      公共布尔shouldOverrideUrlLoading(web视图查看,字符串URL)
       {
         如果(查看== webviewHTML)view.loadUrl(URL);
         返回false;
         //返回super.shouldOverrideUrlLoading(查看,网址);
       }

      @覆盖
      公共无效onReceivedError(web视图来看,INT错误code,字符串描述,字符串failingUrl)
       {
         如果(查看== webviewHTML)ApplicationLeta.fPopup(的getString(R.string.sPopupErrorSiteOpen)+:+说明);
         // ActivityBrowser.this.finish();
       }

      公共无效onReceivedSslError(web视图来看,SslErrorHandler处理器,SslError错误)
       {
         如果(查看== webviewHTML)handler.proceed();
       }
    }


   @覆盖
   公共布尔dispatchTouchEvent(MotionEvent motionEvent)
    {
      super.dispatchTouchEvent(motionEvent);

      如果(motionEvent.getAction()== MotionEvent.ACTION_MOVE)返回true;

      如果(motionEvent.getAction()== MotionEvent.ACTION_UP)
        {
          // 做一点事
        }

      如果(motionEvent.getAction()== MotionEvent.ACTION_UP)
        {
          // 做一点事
        }
      返回false;
    }


   @覆盖
   公共无效onBack pressed()
    {
    }


   @覆盖
   公共无效onWindowFocusChanged(布尔EFOCUS)
    {
      super.onWindowFocusChanged(EFOCUS);
      如果(EFOCUS ==假)
        {
          fKeyboardClose();

          新主题(新的Runnable()
           {
             @覆盖
             公共无效的run()
              {
                尝试
                  {
                    仪表研究所=新规范();
                    inst.sendKeyDownUpSync(KeyEvent.KEY code_BACK);
                  }
                 赶上(例外五){}
              }
           } )。开始();
        }
    }


   私人无效fKeyboardClose()
    {
      InputMethodManager inputMethodManager =(InputMethodManager)getSystemService(Activity.INPUT_METHOD_SERVICE);
      inputMethodManager.hideSoftInputFromWindow(getCurrentFocus()getWindowToken(),0);
    }


   公共OnGlobalLayoutListener onGlobalLayoutListener =新OnGlobalLayoutListener()
    {
      @覆盖
      公共无效onGlobalLayout()
       {
         矩形RECT =新的矩形();
         viewRootHTML.getWindowVisibleDisplayFrame(RECT);
         iViewRootHTMLHeightDifferent = viewRootHTML.getRootView()的getHeight() - (rect.bottom  -  rect.top)。
         如果(iViewRootHTMLHeightDifferent→50)fKeyboardClose();
       }
    };

   @燮pressWarnings(德precation)
   @燮pressLint(SetJavaScriptEnabled)
   公共无效的onCreate(包savedInstanceState)
    {
      super.onCreate(savedInstanceState);
      的setContentView(R.layout.browser);

      如果(savedInstanceState == NULL)
        {
          viewRootHTML = findViewById(R.id.linearLayoutHTML);
          。viewRootHTML.getViewTreeObserver()addOnGlobalLayoutListener(onGlobalLayoutListener);

          webviewHTML =(web视图)findViewById(R.id.webviewHTML);
          WebSettings webSettings = webviewHTML.getSettings();
          webSettings.setJavaScriptEnabled(真正的);
          webSettings.setJavaScriptCanOpenWindowsAutomatically(真正的);
          webviewHTML.setWebViewClient(新wiewClient());
          webviewHTML.loadUrl(ApplicationLeta.sAppInterviewURL);
        }
    }
 }
 

这code也接近系统消息编辑文字/输入法,当用户长preSS上输入字段。

不过!这code关闭键盘后,才打开。键盘保持可见几毫秒和用户(快速用户)可以preSS键盘上的任意键。这是不是最好的情况。

也许存在着100%禁用键盘最好的办法,而不打开它?

解决方案

本答案为我工作(由安卓Weblineindia提供):<一href="http://stackoverflow.com/a/29409478/4813198">http://stackoverflow.com/a/29409478/4813198

  

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

 &GT;
&GT;机器人:descendantFocusability =blocksDescendants
&GT;
 

  

和按照您的WebView属性中设置:

 &GT;机器人:可聚焦=假
&GT;机器人:focusableInTouchMode =真
 

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 http://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): http://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"

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

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