我有一个 Webview,其中加载了用户定义的网站,然后按下 WebView 下方的按钮,将 Javascript(来自远程 url)加载到用户所在的页面中。
然后应该触发这个 JavaScript 中的一个函数,但是它似乎没有工作。
我的活动:
public class SearchViewer extends SherlockActivity {
TextView grabit;
WebView webView;
ImageView right, left;
ProgressBar progressBar;
@SuppressLint({ "JavascriptInterface", "SetJavaScriptEnabled" })
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.searchviewer);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setIcon(R.drawable.ic_arrowleft);
Intent intent = getIntent();
String url = intent.getStringExtra("URL");
String name = intent.getStringExtra("Name");
setTitle(name);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
webView = (WebView) findViewById(R.id.webView1);
webView.loadUrl(url);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}
});
grabit = (TextView) findViewById(R.id.grabit);
grabit.setOnClickListener(OnClick_grabit);
right = (ImageView) findViewById(R.id.right);
left = (ImageView) findViewById(R.id.left);
right.setOnClickListener(OnClick_right);
left.setOnClickListener(OnClick_left);
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
break;
default:
return super.onOptionsItemSelected(item);
}
return false;
}
public void onStart() {
super.onStart();
EasyTracker.getInstance(this).activityStart(this);
}
public void onStop() {
super.onStop();
EasyTracker.getInstance(this).activityStop(this);
}
private OnClickListener OnClick_grabit = new OnClickListener() {
public void onClick(View v) {
webView.loadUrl("javascript: function loadScript(scriptURL) { var scriptElem = document.createElement('SCRIPT'); scriptElem.setAttribute('language', 'JavaScript'); scriptElem.setAttribute('src', scriptURL); document.body.appendChild(scriptElem);} loadScript('http://www.pathtofile.com/javascript.js');");
webView.loadUrl("javascript: initialize("+1+")");
}
};
private OnClickListener OnClick_right = new OnClickListener() {
public void onClick(View v) {
webView.goForward();
}
};
private OnClickListener OnClick_left = new OnClickListener() {
public void onClick(View v) {
webView.goBack();
}
};
当我加载javascript时
webView.loadUrl("javascript: function loadScript(scriptURL) { var scriptElem = document.createElement('SCRIPT'); scriptElem.setAttribute('language', 'JavaScript'); scriptElem.setAttribute('src', scriptURL); document.body.appendChild(scriptElem);} loadScript('http://www.pathtofile.com/javascript.js');");
并在 Javascript 文件中启动 Javascriptinitialize()
函数,它工作正常。但是,当我尝试执行上述两行时
webView.loadUrl("javascript: function loadScript(scriptURL) { var scriptElem = document.createElement('SCRIPT'); scriptElem.setAttribute('language', 'JavaScript'); scriptElem.setAttribute('src', scriptURL); document.body.appendChild(scriptElem);} loadScript('http://www.pathtofile.com/javascript.js');");
webView.loadUrl("javascript: initialize("+1+")");
它不起作用,我怎样才能加载 Javascript,然后从 android 触发函数,以便我可以传递一个值。
我努力了
private OnClickListener OnClick_grabit = new OnClickListener() {
public void onClick(View v) {
webView.loadUrl("javascript: function loadScript(scriptURL) { var scriptElem = document.createElement('SCRIPT'); scriptElem.setAttribute('language', 'JavaScript'); scriptElem.setAttribute('src', scriptURL); document.body.appendChild(scriptElem);} loadScript('http://www.pmdev.co.uk/dadg-giftgrabber/android/gift-grabber.js');");
webView.setWebViewClient(new WebViewClient(){
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
webView.loadUrl("javascript: initialize("+1+")");
}
});
}
};
但这不起作用