2011-06-15 34 views
4
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    Main(); 

} 
public void Main() 
{ 
    _linearLayout = new LinearLayout(this); 
    _webview = new WebView(this); 
    _linearLayout.addView(_webview, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); 
    setContentView(_linearLayout); 

    _webview.getSettings().setJavaScriptEnabled(true); 
    _webview.getSettings().setPluginsEnabled(true); 
    _webview.getSettings().setAllowFileAccess(true); 

    _webview.setWebChromeClient(new WebChromeClient()); 
    _webview.addJavascriptInterface(this, "Question"); 
    _webview.loadData(GetHTML(), "text/html", "utf-8"); 


} 

public String GetHTML() 
{ 
    String HTML = "" 
     + "<HTML>" 
     + "<HEAD>" 
     + "<TITLE>Radio Button onClick Handler</TITLE>" 
     + "<SCRIPT LANGUAGE=\"JavaScript\">" 
     +"function function1(colors) {" 
     +"var col = (colors.options[colors.selectedIndex].value);" 
     +" if (col) {" 
     +" document.bgColor = col;" 

     +" } " 
     +"</script>" 
     + "</HEAD>" 
     + "<BODY>" 
     +"<form>" 
     +"<b> Hello </b>" 
     //+"<select name=\"colors\" onChange=\"window.Question.function1(this);\">" 
     +"<select name=\"colors\" onChange=\"window.Question.OnJsClick_SelectedItem(' string value');\">" 
      +"<option value=\"white\" selected>White</option>" 
      + "<option value=\"cyan\">Cyan</option>" 
      + "<option value=\"ivory\">Ivory</option>" 
      + "<option id=\"myO\" value=\"blue\">Blue</option>" 

     +"</select>" 
     +"</form>" 
     + "</BODY>" 
     + "</HTML>"; 

    return HTML; 
} 

public void OnJsClick_SelectedItem(final String str) 
{ 
    mHandler.post(new Runnable() 
    { 
     //@Override 
     public void run() 
     { 
      getValue(str); 
     } 
    }); 
} 

public String getValue(String str) 
{ 
    _webview.loadUrl("javascript:function1(colors)"); 
    Toast.makeText(this, "Under getValue " + str, Toast.LENGTH_SHORT).show(); 
    return str; 

} 
} 

請幫我一把。在這種情況下如何從Javascript傳遞數據到Android WebView?

+1

你應該接受回答您的問題,如果你(看到有發現他們是有用的在那裏打勾)並使用upvotes。它會幫助你獲得更多答案。 – Rishabh 2011-06-15 11:48:41

回答

2

你應該使用這樣的事情:

<select name="colors" 
    onChange="Question.OnJsClick_SelectedItem(this.options[this.selectedIndex].text)"> 
+0

謝謝巴迪工作得很好..謝謝了很多 – 2011-06-15 12:17:26

+0

不客氣! – Michael 2011-06-15 12:43:35

3

您可以將代碼作爲HTML頁面存儲在assets文件夾中,並使用加載URL方法訪問webview以顯示網頁。

mWebView.loadUrl("file:///android_asset/index.html"); 

編輯根據評論

中啓用JavaScript

WebView webView = (WebView) findViewById(R.id.webview); 
    WebSettings webSettings = myWebView.getSettings(); 
    webSettings.setJavaScriptEnabled(true); 
    webView.addJavascriptInterface(new JavaScriptInterface(this), "Android"); 

這裏的一些HTML和JavaScript創建使用新界面,當用戶點擊一個按鈕敬酒消息

<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" /> 

<script type="text/javascript"> 
    function showAndroidToast(toast) { 
     Android.showToast(toast); 
    } 
</script> 

在Java代碼中添加此

public class JavaScriptInterface { 
    Context mContext; 

    /** Instantiate the interface and set the context */ 
    JavaScriptInterface(Context c) { 
     mContext = c; 
    } 

    /** Show a toast from the web page */ 
    public void showToast(String toast) { 
     Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show(); 
    } 
} 
+0

這很好,我可以從資源文件夾加載HTML頁面並通過loadURL加載。在這裏,我的問題是我想獲得組合框中選定的值Toast.Suppose用戶選擇象牙顏色,然後我的吐司打印象牙選擇。你能幫我嗎? @Rishabh – 2011-06-15 11:53:44

+0

我能夠在webview中加載HTML,但我想通過選擇的值(組合框中選擇的值寫入JavaScript)到java代碼 – 2011-06-15 11:59:56

+0

嘗試編輯代碼出它將使Java代碼集成到JavaScript,HTML。 並接受它如果工作 – Rishabh 2011-06-15 12:03:35