如何从html文件中检测div标签?

时间:2015-01-29 11:42:32

标签: android html webview

我正在网页视图中加载html页面。在HTML中有一个按钮;点击它,我必须开始一个新的活动。我已经显示了html文件,但是如何检测按钮的点击?

按钮的html代码: 为按钮定义了单独的css文件。

<div id="button">
     <ul>
         <li><a title="Forming a Habit" href="Forming_Habit.html">
             More
             <span class="arrows">
                 &nbsp;&#187;
             </span>
         </a></li>
     </ul>
 </div>

加载html的代码:

webView.setWebViewClient(new MyWebViewClient());

        WebSettings webSetting = webView.getSettings();
        webSetting.setJavaScriptEnabled(true);
        webSetting.setPluginState(WebSettings.PluginState.ON_DEMAND);
        //webSetting.setBuiltInZoomControls(true);
        webSetting.setAllowContentAccess(true);

        webSetting.setDefaultTextEncodingName("utf-8");
        webView.loadUrl("file:///android_asset/"+htmlFile);

webViewClient类:

@Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        view.addJavascriptInterface(new Object(){
        return true;
    }

请指导我。

2 个答案:

答案 0 :(得分:0)

使用它:

HTML:

 <div id="button" onclick="Helper.methodeone()">
 <ul>
     <li><a title="Forming a Habit" href="Forming_Habit.html">
         More
         <span class="arrows">
             &nbsp;&#187;
         </span>
     </a></li>
 </ul>

on java:

    webView.setWebViewClient(new MyWebViewClient());
    WebSettings webSetting = webView.getSettings();
    webSetting.setJavaScriptEnabled(true);
    webSetting.setPluginState(WebSettings.PluginState.ON_DEMAND);
    //webSetting.setBuiltInZoomControls(true);
    webSetting.setAllowContentAccess(true);
    web.addJavascriptInterface(new Samlplejavaconect(), "Helper");//NEW LINE
    webSetting.setDefaultTextEncodingName("utf-8");
    webView.loadUrl("file:///android_asset/"+htmlFile);

在下面的课程中,您可以管理活动:

public class Samlplejavaconect {
    public void methodeone() {
        Toast.makeText(this, "Hi", Toast.LENGTH_LONG).show();//FOR EXAMPLE
    }
}

答案 1 :(得分:0)

试试这个

<强> MainActivity.java

public class MainActivity extends Activity {
WebView webView;
JavaScriptInterface jsInterface;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    webView = (WebView) findViewById(R.id.webView1);
    WebSettings webSettings = webView.getSettings();
    // enable JavaScript
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setWebChromeClient(new WebChromeClient() {
    });
    jsInterface = new JavaScriptInterface(MainActivity.this, webView);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.addJavascriptInterface(jsInterface, "AndroidFunction");
    webView.loadUrl("file:///android_asset/book.html");
}

public class JavaScriptInterface {
    private Activity activity;
    WebView wb;

    public JavaScriptInterface(Activity activiy, WebView wb) {
        this.activity = activiy;
        this.wb = wb;
    }
    @JavascriptInterface
    public void activity() {
        try {
            Class activity = Class.forName("com.example.classname");
            Intent myIntent = new Intent(getApplicationContext(), activity);
            startActivity(myIntent);
            finish();

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

}

}

<强> book.html

<!DOCTYPE html>
<html>
<body>
<button type="button" onclick="startactivity()">Click Me!</button>
<script>
function startactivity() {
   AndroidFunction.activity();
}
</script>
</body>
</html>