2010-11-16 23 views
52

Tôi đã tạo WebView của riêng mình và đặt các đối tượng WebChromeClient và WebViewClient. Khi tôi bắt đầu WebView này, các trường biểu mẫu HTML phản ứng khi tôi chạm vào chúng (một con trỏ xuất hiện), nhưng chúng không được chọn, cũng như bàn phím mềm bắt đầu. Nếu tôi sử dụng bi xoay để chọn biểu mẫu và nhấn biểu tượng đó, bàn phím sẽ xuất hiện.Trường khai thác biểu mẫu trong WebView không hiển thị bàn phím mềm

Tôi đã cố gắng gọi số myWebview.requestFocusFromTouch() làm đề xuất this answer nhưng trả về sai và không được trợ giúp.

+0

bạn đã làm điều này chưa? : http://stackoverflow.com/questions/35465569/changing-the-input-source-of-form-fields-in-webview – debonair

Trả lời

94

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

Đây là bản sửa lỗi trong trường hợp không rõ ràng.

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 ... hành vi này có vẻ là một lỗi, nó được sửa trong ICS. – Janusz

+0

@androidchen Cảm ơn bạn rất nhiều ... bạn tiết kiệm thời gian của tôi :) –

+0

Tôi nhận được erros với điều này - nó muốn tôi đổi tên "onTouch" ... tôi phải làm gì ??? Vui lòng giúp – Levchik

3

câu trả lời AndroidChen đã không làm việc cho tôi chút nào, nhưng tôi đã tìm kiếm các liên kết được cung cấp (http://code.google.com/p/android/issues/detail?id=7189) và tôi thấy lớp sau, nó hoạt động một cách hoàn hảo (Android Froyo trên HTC Bravo), không chỉ văn bản, mà còn tất cả các nút, chuyển hướng, vv, tất cả mọi thứ hoạt động hoàn hảo: 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; 
    } 
} 

và sau đó để tạo ra một webview trong một hộp thoại, tôi đã viết mã này

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

Cảm ơn bạn, Dv_MH! Câu trả lời được chấp nhận cũng không hiệu quả đối với tôi, nhưng câu trả lời này hoạt động. –

24

Đồng ý 10% với của Dv_MH câu trả lời. Tuy nhiên, lớp học kèm theo hơi quá mức. Tất cả tôi cần làm là mở rộng WebView & trở lại đúng với onCheckIsTextEditor()

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

Từ đó, tôi đã có thể sử dụng nó như một WebView bình thường (ngay cả trong Dialogs).

Ngoài ra, với mã ít:

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

Đây là người duy nhất thực sự làm việc cho tôi. Mọi thứ khác đều bỏ qua bàn phím mềm. – kingargyle

+0

Điều này làm việc cho tôi (và không phải là câu trả lời đầu tiên) trên 'DialogFrament' – David

+0

Điều này cũng phù hợp với tôi. Tôi đã làm nó một chút khác nhau –

4

QUAN TRỌNG Tôi đã mất nhiều giờ tìm kiếm này và tôi giải quyết nó bằng cách đảm bảo rằng bố trí cha mẹ mà kéo dài "ViewGroup" không có tài sản

android: descendantFocusability = "blocksDescendants" ngăn chặn bố cục con khỏi tập trung

HOẶC Thêm android: đặt tiêu điểm = "true" để WebView

+0

tìm kiếm dự án của tôi cho "descendantFocusability" đã xóa dòng đó và cuối cùng nó đã hoạt động. Đã không ở trong một webView và tôi không chắc chắn làm thế nào mà dòng kết thúc trong mã nguồn. – chaggy

1

Chỉ cần thêm EditText vô hình dưới WebView

<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"/> 

Tất cả những giải pháp trọng tâm liên quan đến không làm việc trên tôi Samsung Note 3/Android 5.0

Các vấn đề liên quan