简体   繁体   中英

how to detect div tag from html file?

I am loading html page in web view. In html there is a button; on click of that, I have to start a new activity. I have displayed html file but how to detect the click of button ?

html code for button: There is separate css file defined for button.

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

code for loading 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 class:

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

Please guide me.

use this :

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

And in below class you can manage activity :

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

Try this

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM