2012-12-17 37 views
2

我是Android應用開發新手。我可以將HTML頁面的特定值傳遞給我的活動嗎?

我有一個查詢。我可以將Value從我的HTML頁面傳遞給我的活動文件嗎?位於資產/ WWW文件夾,並放在src /包名

任何解決方案或建議的活動文件

HTML文件,請重播我。我不怎麼這樣做。所以請幫助我做到這一點。

在此先感謝。

回答

7

您需要使用JavaScriptInterface。在你的網頁視圖中添加這個接口。喜歡(在這裏,你可以使用任何名稱爲類)

  1. 製作JavaScriptInterface類

    public class JavaScriptInterface { 
        Context mContext; 
    
        /** Instantiate the interface and set the context */ 
        JavaScriptInterface(Context context) { 
         mContext = context; 
        } 
    
        /** Get passed value from the web page here */ 
        public void showMyValue(String passedValue) { 
         android.util.Log.i("TAG", "I Got this value:" + passedValue); 
        } 
    } 
    
  2. 在你的WebView添加這個接口像

    WebView webView = (WebView) findViewById(R.id.webview); 
    webView.addJavascriptInterface(new JavaScriptInterface(this), "Android"); 
    
  3. 現在,在您的網頁,調用這種方法來傳遞你的價值,並做一些需要充分的價值

    <input type="button" value="ClickMe" onClick="passValueToAndroid('Hello Android!')" /> 
    <script type="text/javascript"> 
    function passValueToAndroid(yourPassingValue) { 
        Android.showMyValue(yourPassingValue); 
    } 
    </script> 
    

我希望這可以幫助你。

1

是的,你可以將任何變量從html傳遞給activity。

您需要創建JavaScript接口HTML之間互動的活動,

請參閱此鏈接,實現細節

http://developer.android.com/guide/webapps/webview.html

我希望上面的鏈接解決您的問題。

+0

感謝您的幫助... – PSK

+0

我沒有得到確切的解決方案。你能舉一個小例子嗎?請。 – PSK

相關問題