À¥ ºä¿¡¼ ÅÂ±× Á¤º¸ ÃàÃâÇϱâ(Get the information of html tag in WebView of android) |
|
|
 |
8³â Àü |
À¥ ºä¿¡¼ ÅÂ±× Á¤º¸ ÃàÃâÇϱâ (ÀÚ¹Ù-ÀÚ¹Ù½ºÅ©¸³Æ® ÀÎÅÍÆäÀ̽º)
1. ÅÍÄ¡ ¿µ¿ª¿¡¼ Á¤º¸ ¾ò±â
private View.OnTouchListener webviewTouchListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
WebView.HitTestResult hr = ((WebView)v).getHitTestResult();
if(hr != null) Log.d(TAG, "getExtra = "+ hr.getExtra() + "\t\t Type=" + hr.getType());
return false;
}
};
2. ÀÚ¹Ù½ºÅ©¸³Æ®¸¦ ÀÌ¿ëÇÏ´Â ¹æ¹ý(addJavascriptInterface)
¿¹½Ã·Î ƯÁ¤¹öÆ°À» id °ªÀ¸·Î ãÀº ´ÙÀ½¿¡ ±× href ¼Ó¼º°ªÀ» ¾ò´Â °úÁ¤ÀÌ´Ù.
//ÀÚ¹Ù½ºÅ©¸³Æ®¿¡¼ ¾²ÀÏ Å¬·¡½º
private class JsObject {
@JavascriptInterface
public void saveButtonLink(String link) {
//¹öÆ° ÁÖ¼ÒÀÓ
Log.d(TAG, "===: buttonLink > " + link);
}
}
//À¥ Ŭ¶óÀ̾ðÆ® Ŭ·¡½º
private class CustomClient extends WebViewClient {
@Override
public void onPageFinished(WebView view, String url) {
//btn1 ¿¤¸®¸ÕÆ®¿¡¼ href °ªÀ» Àü´Þ
webview.loadUrl("javascript:var element = document.getElementById('btn1');"
+ " var link = element.getAttribute('href');"
+ " injectedObject.saveButtonLink(link)");
super.onPageFinished(view, url);
}
}
//Ŭ¶óÀ̾ðÆ® »ý¼º
CustomClient client = new CustomClient();
//À¥ºä
webview = (WebView) view.findViewById(R.id.webview);
//ÀÚ¹Ù½ºÅ©¸³Æ® »ç¿ë
webview.getSettings().setJavaScriptEnabled(true);
//Ŭ¶óÀ̾ðÆ® ¼³Á¤
webview.setWebViewClient(client);
//ÀÚ¹Ù½ºÅ©¸³Æ® ÀÎÅÍÆäÀ̽º ¼³Á¤
webview.addJavascriptInterface(new JsObject(), "injectedObject");
|
|
̵̧ : 364 |
̵̧
¸ñ·Ï
|
|