Android UI uses HTML layout (Opens Server Web page directly)

Source: Internet
Author: User

Most of the time we use HTML layout to be more convenient and direct, record a bit.

I now mainly directly invoke the server's Web page (actually JSP, just return HTML), so need to network, the first step to add permissions.

<uses-permission android:name= "Android.permission.INTERNET"/>

The layout file is directly used with a webview, as follows:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" match_parent "    android:o rientation= "vertical" >    <webview        android:id= "@+id/webview1" android:layout_width= "Match_        Parent "        android:layout_height=" Match_parent "/></linearlayout>

You can write the code directly below:

Package Com.yangshidesign.testgryoscope;import Android.app.activity;import Android.os.bundle;import Android.util.log;import Android.webkit.javascriptinterface;import Android.webkit.webview;import Android.widget.toast;public class Addemojiactivity extends Activity {private WebView WebView; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); This.setcontentview (r.layout.activity_ Addemoji); WebView = (WebView) This.findviewbyid (r.id.webview1); Webview.getsettings (). Setjavascriptenabled (True); Webview.addjavascriptinterface (New Webplugin (), "Webplugin"); Webview.loadurl (This.getstring (R.string.server_url) );} /** * Plug-in class, in the HTML JS inside directly call */private class Webplugin {@JavascriptInterfacepublic void Test () {LOG.E ("Miquan", "kkkkkk"); Toast.maketext (addemojiactivity.this, "Test Toast", Toast.length_short). Show (); @JavascriptInterfacepublic String test2 () {return ' Something ';}}}

one of the @javascriptinterface annotations is added to each method that needs to be used.

Finally, you can call it directly on the HTML page.

<script type= "Text/javascript" >function Test () {webplugin.test (); var something = Webplugin.test2 ();} </script>




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.