从 Activity 类调用 Web 视图中的 JavaScript 函数
编辑:这个剪辑最终成功了。我之前曾尝试过此操作,但实际上我的 javascript 遇到了范围界定问题。我在 document.onready 中定义了 appBack() 。只需将该函数的作用域重新调整为 *window.*appBack = function()。生意做完了。希望这对某人有帮助。
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
WebView.loadUrl("javascript:appBack()");
return true;
}
return super.onKeyDown(keyCode, event);
}
在 webview 应用程序中,后退导航是使用自定义 JS 函数 appBack() 处理的,我一直在尝试找出一种方法来拦截 android 物理后退按钮并调用该 javascript 函数。这是我的活动文件。它非常基本,设置一个网络视图并侦听后退按钮单击。目前,当用户单击此物理后退按钮时,它会运行 mWebView.goBack()。这是我想执行 javascript:appBack() 的地方
package com.stgeorgeplc.app;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
public class StGeorgePLCliteActivity extends Activity {
/** Called when the activity is first created. */
WebView mWebView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.loadUrl("file:///android_asset/www/index.html");
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
Edit: This snipped worked in the end. I had been trying this before but was actually experiencing a scoping issue with my javascript. I had appBack() defined in document.onready. Simply re-scoping that function to *window.*appBack = function(). Done the business. Hope this helps someone.
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
WebView.loadUrl("javascript:appBack()");
return true;
}
return super.onKeyDown(keyCode, event);
}
Within a webview application the back navigation is handled with a custom JS function appBack() I've been trying to figure out a way to intercept the android physical back button and call that javascript function instead. Here is my activity file. Its very basic, sets up a webview and listens for the back button click. Currently when the user clicks this physical back button it runs mWebView.goBack(). Which is where I would like to perform javascript:appBack()
package com.stgeorgeplc.app;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
public class StGeorgePLCliteActivity extends Activity {
/** Called when the activity is first created. */
WebView mWebView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.loadUrl("file:///android_asset/www/index.html");
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论