2014-01-22 130 views
0

在这段代码中,我返回一个字符串。我想在android活动中存储这个字符串。我如何调用JavaScript函数并从android活动中获取返回值?如何调用JavaScript函数并从android获取返回值?

<script type="text/javascript"> 
     function gettext(){ 
      return 'http://192.168.1.11/bmahtml5/images/specs_larg_2.png'; 
     }; 
     function button_clicked(){ 
      window.location='ios:nativecall'; 
     }; 
</script> 
+0

我可以为KNW重量parpose你必须使用它? –

+0

双。 http://stackoverflow.com/questions/7544671/how-to-call-javascript-from-android –

+0

实际上,我加载HTML页面,当我点击一些部分时,我不得不打电话thi gettext函数返回图像的链接和我将此图像存储在特定变量中,并在图像视图中显示图像。 – ishu

回答

0

这里是一个简单:

WebAppInterface.java

public class WebAppInterface 
{ 
     Context mContext; 

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

     public void storeText(String text) 
     { 
      //TODO save text 
      Toast.makeText(mContext, text, Toast.LENGTH_LONG).show(); 
     } 
    } 

WebView webview=(WebView)findViewById(R.id.webview); 
webview.getSettings().setJavaScriptEnabled(true); 
webview.addJavascriptInterface(new WebAppInterface(this), "Android"); 

现在你可以调用HTML这种方法,看它

<script type="text/javascript"> 

    function saveFunction(text) 
    { 
     Android.storeText(text); 
    } 
</script> 
+0

您好thnks我试过代码在你的脚本函数android.stroretext函数callls和在我的脚本函数只是返回字符串值我怎么能得到这个字符串 – ishu

+0

尝试它,可以执行操作,但不会返回一个值。 webview.loadUrl(“javascript:gettext()”); – hoafer

相关问题