【发布时间】:2011-03-30 10:49:36
【问题描述】:
我无法使用 WebView 将点击和滑动事件绑定到我的应用程序中,并使用在浏览器中运行良好的简单示例代码。我正在使用 JQuery 移动。有什么想法吗?
<!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 不应该像网络浏览器一样工作吗?
【问题讨论】:
-
提示:有比 1.5 更新的 jQuery 版本(1.5.1 和 1.5.2)
标签: android jquery-mobile android-webview