2011-03-30 81 views
0

我無法使用WebView和瀏覽器中正常工作的簡單示例代碼,在我的應用中綁定點擊和滑動事件。我正在使用JQuery Mobile。有任何想法嗎?Android 2.2上WebView中的綁定事件

<!DOCTYPE html> 
<html> 
<head> 
<title>jQuery Mobile Events</title> 
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0a3/jquery.mobile-   1.0a3.min.css" /> 
<script src="http://code.jquery.com/jquery-1.5.min.js"></script> 
<script src="http://code.jquery.com/mobile/1.0a3/jquery.mobile-1.0a3.min.js"></script> 
<script type="text/javascript"> 
$(function() { 
$('body').bind('taphold', function(e) { 
alert('You tapped and held!'); 
e.stopImmediatePropagation(); 
return false; 
}); 
$('body').bind('swipe', function(e) { 
alert('You swiped!'); 
e.stopImmediatePropagation(); 
return false; 
}); 
}); 
</script> 
</head> 
<body> 
<div data-role="page" id="home"> 
<div data-role="header"> 
<h1>jQuery Mobile Events</h1> 
</div> 
<div data-role="content"> 
<p>Try:</p> 
<ul> 
    <li>Tapping and holding</li> 
    <li>Swiping</li> 
</ul> 
</div> 
</div> 
</body> 
</html> 

這是我在Java代碼:

WebView webview; 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    webview = (WebView) findViewById(R.id.webview); 
    webview.getSettings().setJavaScriptEnabled(true); 
    webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY); 
    webview.loadUrl("web-page-here"); 
    webview.addJavascriptInterface(new JavaScriptInterface(this), "Android"); 
    webview.setWebViewClient(new WebViewClient()); 
} 

這裏的一個Javascript接口:

import java.io.IOException; 
import android.content.Context; 
import android.widget.Toast; 

public class JavaScriptInterface { 
Context mContext; 
AudioRecord audiorecord; 
/** 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(); 
} 

// OnSwipe() here? 
} 

所以我應該在此處添加方法和檢測刷卡時,它應該被稱爲在JQuery與Android.OnSwipe()?爲什麼上面的JQuery Mobile代碼不能用於導航? WebView不應該像Web瀏覽器一樣工作嗎?

+0

只是一個提示:有一個比1.5(1.5.1和1.5.2)更新的jQuery版本, – naugtur 2011-03-30 11:55:51

回答

0
  1. 你應該告訴你如何實現JavaScriptInterface
  2. 似乎沒有被從JS任何傳播,即使你有,你的實際的Android JS的橋樑。在JS代碼中,您應該調用類似Android.onSwipe()並在JavaScriptInterface中實現onSwipe。