2010-08-11 88 views
40

當我顯示WebView時,我看不到彈出的軟鍵盤。硬鍵盤也不起作用!WebView textarea不會彈出鍵盤

有什麼常見的缺點。

,我用它來訪問代碼WebView是:

package com.example.blahblah; 

import android.app.Activity; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.os.Bundle; 
import android.os.Handler; 
import android.preference.PreferenceManager; 
import android.util.Log; 
import android.view.KeyEvent; 
import android.view.Window; 
import android.webkit.JsResult; 
import android.webkit.WebChromeClient; 
import android.webkit.WebSettings; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.widget.Toast; 

public class createAccount extends Activity { 

private static final String LOG_TAG = "Create Account"; 
private WebView mWebView; 
private static final String URL = blah_app.urlSelected+"createAccount.html";  
private Handler mHandler = new Handler(); 

@Override 
public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    Toast.makeText(createAccount.this, "URL = " +URL, Toast.LENGTH_SHORT).show(); 
    getWindow().requestFeature(Window.FEATURE_PROGRESS); 
    setContentView(R.layout.webview); 
    getWindow().setFeatureInt(Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON); 

    mWebView = (WebView) findViewById(R.id.webview); 

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


    final Activity activity = this; 
    mWebView.setWebChromeClient(new WebChromeClient() { 
    public void onProgressChanged(WebView view, int progress) { 

     activity.setProgress(progress * 1000); 
     } 
    }); 

    mWebView.addJavascriptInterface(new DemoJavaScriptInterface(), "demo"); 
    mWebView.clearCache(true); 
    setProgressBarVisibility(true); 
    mWebView.setWebViewClient(new WebViewClient() { 
      public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 
      Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show(); 
      } 

      @Override 
      public void onPageFinished(WebView view, String url) 
      { 
       mWebView.loadUrl("javascript:(function() { " + 
         "setVariables("+blah_app.numberSelected+",'"+blah_app.urlSelected+"');" + 
         "})()"); 
      } 
     }); 

    mWebView.loadUrl(URL); 
} 

final class DemoJavaScriptInterface { 

    public void setData(String fname, String lname, String gacct, String phone) { 

     SharedPreferences prefCreateAccount = PreferenceManager.getDefaultSharedPreferences(createAccount.this); 
     SharedPreferences.Editor editor = prefCreateAccount.edit(); 
     editor.putString("FirstName", fname); 
     editor.putString("LastName", lname); 
     editor.putString("GmailAcct", gacct); 
     editor.putString("Phone", phone); 
     editor.commit();  

    } 
    DemoJavaScriptInterface() { 

    } 

    /** 
    * This is not called on the UI thread. Post a runnable to invoke 
    * loadUrl on the UI thread. 
    */ 
    public void clickOnAndroid() { 
     mHandler.post(new Runnable() { 
      public void run() { 
       mWebView.loadUrl("javascript:wave()"); 
      } 
     }); 
    } 
} 

/** 
* Provides a hook for calling "alert" from javascript. Useful for 
* debugging your javascript. 
*/ 
final class MyWebChromeClient extends WebChromeClient { 
    @Override 
    public boolean onJsAlert(WebView view, String url, String message, JsResult result) { 
     Log.d(LOG_TAG, message); 
     result.confirm(); 
     return true; 
    } 
} 

public boolean onKeyDown(int keyCode, KeyEvent event) { 
    if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) { 
     startActivity(new Intent(getApplication(), blah_app.class)); 
     return true; 
    } 
    return super.onKeyDown(keyCode, event); 
} 
} 
+0

你的意思是說,當你在網頁上,或者當你點擊一個textarea時,鍵盤不會彈出? – Sephy 2010-08-11 17:19:01

+0

也許他意味着在webview中顯示的網站內的html輸入字段? – 2010-08-11 17:28:46

+0

@Sephny: 當我點擊textarea時,鍵盤不會彈出,也不能在Motorola Milestone上使用硬鍵盤! – Sana 2010-08-11 17:32:56

回答

37

的問題是,當它被使用

webView.requestFocus(View.FOCUS_DOWN); 

解決了這個問題,因此加載的WebView沒有得到焦點。

+0

另請參閱:http://code.google.com/p/android/issues/detail?id=7189 – 2011-01-20 16:17:32

+2

此解決方案對於果凍豆的nexus 7沒有幫助,任何人有其他想法? – user1159819 2012-09-16 02:40:31

+0

是的!好的,非常感謝你! :) – user280109 2013-01-04 18:16:25

0

我也有類似的問題....後來我發現,我的顯示Web視圖網頁上的文本框被禁用。

檢查此頁面是否在瀏覽器中存在同樣的問題?

47

完整的解決方案比薩那有點多。據記載作爲Android的網站(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; 
    } 
}); 
+5

它不起作用... – Hugo 2013-03-13 09:20:42

+0

它爲我工作,沒有這一行:'webView.requestFocus(View.FOCUS_DOWN);'。 – Tae 2013-08-07 20:11:05

0

我是有問題的上述解決方案均未完全相同的工作對我來說。經過多年的努力,我終於找到了問題。

我用WebView呈現的各種網頁中的一些不適合Web視圖,因此div(或其他html組件)被無形地放置在輸入字段上。雖然輸入字段在觸摸時顯示爲選中狀態,但它們不允許輸入文字(即使我設法使用跟蹤球來獲得軟鍵盤)。

所以解決方案。

WebView.getSettings().setUseWideViewPort(true); 

這並不能完全阻止問題,但使得視圖更像是PC瀏覽器的網站設計。覆蓋層變化較少。

希望這可以幫助你。

8

即使在Android 4.1上(在SGS3上測試),我仍然感到驚訝,但問題仍然存在,並且覆蓋onTouch()不會爲我解決問題。

測試代碼:

String HTML = "<html><head>TEST</head><body><form>"; 
HTML += "<INPUT TYPE=TEXT SIZE=40 NAME=user value=\"your name\">"; 
HTML += "</form></body></html>"; 

WebView wv = new WebView(getContext()); 
wv.loadData(HTML, "text/html", null); 

final AlertDialog.Builder adb = new AlertDialog.Builder(getContext()); 
adb.setTitle("TEST").setView(wv).show(); 

我複雜的解決方案是MyWebView取代的WebView:

private static class MyWebView extends WebView 
{ 
    public MyWebView(Context context) 
    { 
     super(context); 
    } 

    // Note this! 
    @Override 
    public boolean onCheckIsTextEditor() 
    { 
     return true; 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent ev) 
    { 
     switch (ev.getAction()) 
     { 
      case MotionEvent.ACTION_DOWN: 
      case MotionEvent.ACTION_UP: 
       if (!hasFocus()) 
        requestFocus(); 
      break; 
     } 

     return super.onTouchEvent(ev); 
    } 
} 
+0

這是工作。坦克! – Hugo 2013-03-13 09:29:41

+3

謝謝!這是爲我工作的唯一解決方案。 – jbc25 2013-05-18 09:27:52

+1

在Jelly Bean +中,其他解決方案都沒有工作。這應該是被接受的答案! – 2014-08-12 16:26:06

1

我對果凍豆同樣的問題,但沒有上述解決方案爲我工作。此解決方案爲我工作在果凍豆

WebView webView = new WebView(getActivity(), null, android.R.attr.webViewStyle); 

有時android.R.attr.webViewStyle默認情況下不應用。這確保了風格始終適用。

6

一行答案,它是工作很好

// define webview for browser 
wb = (WebView) findViewById(R.id.webView1); 
wb.requestFocusFromTouch(); 

哪裏wb是我的對象webView

4

有同樣的問題和非上述解決方案的工作。這爲我啓動

<CustomWebView 
    android:id="@+id/webView" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:focusable="true" 
    android:focusableInTouchMode="true" /> 
+1

很難過,我嘗試了所有上述答案後嘗試了您的選擇! – amalBit 2014-02-04 15:42:12

+0

對我來說,這是唯一的解決辦法。我在運行時設置它 - > setFocusable(true); setFocusableInTouchMode(true); – 2016-05-24 12:36:59

0

試試這個 - > firstname是字段的名稱,並確保它沒有自動聚焦屬性。

mWebView.setWebViewClient(new WebViewClient() { 
     @Override 
     public void onPageFinished(WebView view, String url) { 
      mWebView.loadUrl("javascript:document.getElementById(\"firstname\").focus();"); 
     } 
    }); 
0

爲了容易,和其他的解決方案具有不沾代碼在所有設備上或在所有條件下工作時,我總是用一個小黑客來克服這個問題的問題,只需添加任何隱藏editText佈局包含webView將自動給予重點的全貌,你將不必擔心webView文本字段了:

<EditText 
    android:id="@+id/kb_holder" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:inputType="text" 
    android:maxLines="1" 
    android:visibility="gone" /> 

所以,整個佈局是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <EditText 
     android:id="@+id/kb_holder" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:inputType="text" 
     android:maxLines="1" 
     android:visibility="gone" /> 

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

</FrameLayout>