2

私の情報源:

    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;
        }

現在の Web ページを取得するには?

4

1 に答える 1

2

1 つの外部 JavaScript ファイルを作成し、その中に次のコードを記述します。

<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ページに含めます。

この後、onCreate メソッドに次のコードを記述します。

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");
}
}
于 2012-11-21T10:57:00.293 に答える