Android:如何在java代码中覆盖webview中的javascript事件
Title:如何通过点击Android的webView中的一个元素打开native的界面(activity)
如何在webView中直接打开native的界面
设置webview支持javascript
- webview.getSettings().setJavaScriptEnabled(true);
- webview.addJavascriptInterface(new JavascriptInterface(MyActivity.this), "mynamespace");
- public class JavascriptInterface {
- private Activity activity;
- public JavascriptInterface(Activity activity){
- this.activity = activity;
- }
- public void toMyPage(){
- Intent intent = new Intent();
- intent.setClass(activity, AnotherActivity.class);
- activity.startActivity(intent);
- }
- }
- webview.setWebViewClient(new MyWebViewClient(this, browser));
在MyWebViewClient的onPageFinished方法中注入javascript代码
- public class MyWebViewClient extends WebViewClient {
- private Context con;
- public PTCWebViewClient(Context con, final WebView web){
- this.con = con;
- }
- @Override
- public boolean shouldOverrideUrlLoading(WebView view, String url) {
- view.loadUrl(url);
- return true;
- }
- @Override
- public void onPageStarted(WebView view, String url, Bitmap favicon) {
- super.onPageStarted(view, url, favicon);
- }
- @Override
- public void onPageFinished(WebView view, String url) {
- super.onPageFinished(view, url);
- view.loadUrl("javascript:(function(){document.getElementById('snapNSendBtn').onclick=function(){window.ltamtc.toSnapNSend();}})()");
- }
- }
相关推荐
dangai00 2020-07-18
xhpscdx 2020-05-31
chenxiangpeng 2020-04-30
ncuboy0wsq 2020-03-27
学习web前端 2020-03-23
APCDE 2020-03-04
歆萌 2020-03-03
csdnuuu 2020-01-04
水龙吟的备忘录 2019-11-18
icewizardry 2014-04-10
元元 2019-11-17
androidstudyroom 2019-11-08
czpaex 2011-09-11
OldSoldier 2011-08-18
wangdaiying 2011-05-26
qixiang0 2019-08-24