2010-11-16 44 views
52

我创建了自己的WebView并设置了WebChromeClient和WebViewClient对象。当我启动这个WebView时,当我触摸它们(光标出现)时,HTML表单域会作出反应,但它们不会被选中,软键盘也不会启动。如果我使用轨迹球选择窗体并按下它,键盘确实会出现。在WebView中点击表单域并不显示软键盘

我试图拨打myWebview.requestFocusFromTouch()作为this answer建议,但它返回false并没有帮助。

+0

你有没有做到这一点? :http://stackoverflow.com/questions/35465569/changing-the-input-source-of-form-fields-in-webview – debonair 2016-02-17 21:49:46

回答

94

http://code.google.com/p/android/issues/detail?id=7189

这是在其他情况下,修复并不清楚。

webview.requestFocus(View.FOCUS_DOWN); 
    webview.setOnTouchListener(new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      switch (event.getAction()) { 
       case MotionEvent.ACTION_DOWN: 
       case MotionEvent.ACTION_UP: 
        if (!v.hasFocus()) { 
         v.requestFocus(); 
        } 
        break; 
      } 
      return false; 
     } 
    }); 
+0

Thx ...这种行为似乎是一个错误,它是固定在ICS。 – Janusz 2011-12-20 13:03:26

+0

@androidchen非常感谢你...节省我的时间:) – 2013-01-19 10:10:30

+0

我得到了错误 - 它要我重命名“onTouch”...我该怎么做?请帮助 – Levchik 2015-11-04 22:45:50

3

AndroidChen的答案没有工作,为我所有,但我搜索(http://code.google.com/p/android/issues/detail?id=7189)提供的链接,我发现下面的类,它完美的作品(Android的升级Froyo的HTC布拉沃),而不仅仅是文字,还所有的按钮,重定向等,一切完美的作品:d

class LiveWebView extends WebView 
{ 
    Context mContext; 

    public LiveWebView(Context context, String URL) 
    { 
     super(context); 
     mContext = context; 
     setWebViewClient(URL); 
    } 

    @Override 
    public boolean onCheckIsTextEditor() 
    { 
     return true; 
    } 

    @SuppressLint("SetJavaScriptEnabled") 
    boolean setWebViewClient(String URL) 
    { 
     setScrollBarStyle(SCROLLBARS_INSIDE_OVERLAY); 
     setFocusable(true); 
     setFocusableInTouchMode(true); 
     requestFocus(View.FOCUS_DOWN); 

     WebSettings webSettings = getSettings(); 
     webSettings.setSavePassword(false); 
     webSettings.setSaveFormData(false); 
     webSettings.setJavaScriptEnabled(true); 
     webSettings.setSupportZoom(false); 
     webSettings.setUseWideViewPort(false); 

     setOnTouchListener(new View.OnTouchListener() 
     { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) 
      { 
       switch (event.getAction()) 
       { 
        case MotionEvent.ACTION_DOWN: 
        case MotionEvent.ACTION_UP: 
         if (!v.hasFocus()) 
         { 
          v.requestFocus(); 
         } 
         break; 
       } 
       return false; 
      } 
     }); 

     this.setWebViewClient(new WebViewClient() 
     { 
      ProgressDialog dialog = new ProgressDialog(mContext); 

      @Override 
      public boolean shouldOverrideUrlLoading(WebView view, String url) 
      { 
       loadUrl(url); 

       return true; 
      } 

      public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) 
      { 
       Toast.makeText(mContext, "Oh no! " + description, Toast.LENGTH_SHORT).show(); 
      } 

      public void onPageStarted(WebView view, String url, Bitmap favicon) 
      { 
       if (dialog != null) 
       { 
        dialog.setMessage("Loading..."); 
        dialog.setIndeterminate(true); 
        dialog.setCancelable(true); 
        dialog.show(); 
       } 

      } 

      public void onPageFinished(WebView view, String url) 
      { 
       if (dialog != null) 
       { 
        dialog.cancel(); 
       } 
      } 
     }); 

     this.setWebChromeClient(new WebChromeClient() 
     { 
      public void onProgressChanged(WebView view, int progress) 
      { 
       // Activities and WebViews measure progress with different scales. 
       // The progress meter will automatically disappear when we reach 100% 
      } 

      @Override 
      public boolean onJsAlert(WebView view, String url, String message, JsResult result) 
      { 
       result.confirm(); 
       return true; 
      } 
     }); 

     loadUrl(URL); 

     return true; 
    } 
} 

,然后在对话中创建一个web视图,我写了这个代码

AlertDialog.Builder alert = new AlertDialog.Builder(M_PaymentOptions.this); 
alert.setNegativeButton("Back to Payment Options", new DialogInterface.OnClickListener() 
{ 
    @Override 
    public void onClick(DialogInterface dialog, int id) 
    {} 
}); 

alert.setTitle("BarclayCard Payment"); 

LiveWebView liveWevViewObject = new LiveWebView(M_PaymentOptions.this, redirecturl); 

alert.setView(liveWevViewObject); 

alert.show(); 
+0

谢谢,Dv_MH!接受的答案也不适用于我,但是这个工作。 – 2014-05-12 08:48:12

24

同意10%的Dv_MH的回答。但是,所附的课程有点过分。所有我需要做的是延长的WebView &为onCheckIsTextEditor()返回true

import android.content.Context; 
import android.util.AttributeSet; 
import android.webkit.WebView; 

public class LiveWebView extends WebView { 

    public LiveWebView(Context context) { 
     super(context); 
    } 

    public LiveWebView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public LiveWebView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    @Override 
    public boolean onCheckIsTextEditor() { 
     return true; 
    } 
} 

从那里,我可以使用它作为一个正常的WebView(甚至对话框)。

或者,用更少的代码:

WebView web = new WebView(context) { 
    @Override 
    public boolean onCheckIsTextEditor() { 
    return true; 
    } 
}; 
+1

这是唯一真正为我工作的人。其他一切都忽略了软键盘。 – kingargyle 2014-08-06 15:10:33

+0

这对我来说(而不是第一次回答)'DialogFrament' – David 2014-10-12 13:59:54

+0

这也适用于我。我做了一点不同 – 2014-10-15 04:17:38

4

重要 我花了几个小时寻找这一点,我通过确保解决它所有扩展“的ViewGroup”没有财产

父布局

android:descendantFocusability =“blocksDescendants”防止儿童布局变得集中

添加机器人:可调焦= “true” 将的WebView

+0

搜索我的项目“DescendantFocusability”删除该行,它终于奏效。不是在一个webView中,我不知道如何结束在源代码中的行。 – chaggy 2016-11-15 18:06:23

1

只需添加的WebView下看不见的EditText

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<EditText 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:visibility="invisible" /> 

<WebView 
    android:id="@+id/webView" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"/> 

所有这些焦点相关的解决方案,并没有对我的三星工作的注意事项3/Android 5.0

相关问题