【问题标题】:Binding events in WebView on Android 2.2在 Android 2.2 上的 WebView 中绑定事件
【发布时间】: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


【解决方案1】:
  1. 你应该展示你是如何实现 JavaScriptInterface 的
  2. 似乎没有任何从 JS 到实际 Android JS 桥的传播。在 JS 代码中,你应该调用 Android.onSwipe() 之类的东西,并在 JavaScriptInterface 中实现 onSwipe。

【讨论】:

    猜你喜欢
    • 2013-02-06
    • 2020-07-20
    • 1970-01-01
    • 1970-01-01
    • 2011-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-18
    相关资源
    最近更新 更多