本文介绍了DroidGap:如何让网页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的源:

    public class MainActivity extends DroidGap {
    @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            super.loadUrl("file:///android_asset/www/index.html");
    }


    @Override
        public boolean dispatchTouchEvent(MotionEvent e) {
            super.dispatchTouchEvent(e);
    // Now what page ??

            return true;
        }

如何获得当前网页?

How to get the current web page?

推荐答案

创建一个外部JavaScript文件,下面写在里面code:

Create one external javascript file, write following code in it:

<script>
document.getElementByTagName("body").addEventListener('touchstart',touchPagePressed);// you can use 'onclick' also
function touchPagePressed()
{
   alert(touchPagePressed');
   var sPath=window.location.pathname; 
   var sPage = sPath.substring(sPath.lastIndexOf('/') + 1); 
   alert(sPage); 
   MyAndroid.performClick(sPage);
}
</script>

包括外部JS在每一个HTML页面文件。

include that external js file in every html page.

在此之后,下面写在你onCreate方法code:

after this, write following code in your onCreate method:

public class MainActivity extends DroidGap {
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.loadUrl("file:///android_asset/www/index.html");

        WebView webView = (WebView) findViewById(R.id.webView);
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webView.addJavascriptInterface(new Object()
             {
                public void performClick(String currentPageName)
                {
                   // Deal with a click on the body tag
                   System.out.println("PageName- "+currentPageName);  
                }
             },"MyAndroid");
}
}

这篇关于DroidGap:如何让网页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 02:58