How to use a Web page to develop your own app, a button in a Web page that binds to your own Java code to implement a call that JavaScript code calls Java code, and Java code to invoke JavaScript code

Source: Internet
Author: User



1 First is how to use the Web page in your own app, so that you can quickly update the interface without having to upgrade the client, the method is as follows:



XML file:


<WebView 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/web_v"
       
        />


2 binding Web pages in activity:


public class MainActivity extends ActionBarActivity {

private WebView web;
    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.activity_main);
        web = (WebView) findViewById (R.id.web_v);
        
        // equivalent to the browser
        WebSettings webset = web.getSettings ();
        // Can set web properties
        webset.setJavaScriptEnabled (true);
        // The following is the method called by the javascript in the webpage, that is, the demo is the class name that defines the anonymous inside, which implements the javascript in the webpage
        // method to call
        web.addJavascriptInterface (new Object () {// Provide the use interface for JavaScript code in the web control
      @JavascriptInterface // pay attention to this
      public void callous ()
          {
      System.out.println ("hahahahaha");
          Intent intent = new Intent ();
          intent.setAction (Intent.ACTION_CALL);
          intent.setData (Uri.parse ("tel:" + "123123"));
          startActivity (intent);
          }
        }
        , "demo");
        String url = new String ("http://192.168.61.173:8080/myweb/zp.html");
        // Load the web page into the web control
web.loadUrl (url);
        }
    }


3, the page inside the button to use the hyperlink, the method is as follows:


<p><a onclick= "window.demo.callous ()" > Contact Us </a></p>


Where the demo is the class name, callous is the method of the demo class, the front window is the tag, plus not so-called. If there is href= "" then open this page by default, the browser will start, so do not href= "" this property.



4. How to invoke JavaScript functions in Java code to display the original hidden content:



(1) First add a control to the app:


<LinearLayout
         xmlns: android = "http://schemas.android.com/apk/res/android"
     xmlns: tools = "http://schemas.android.com/tools"
     android: layout_width = "match_parent"
     android: layout_height = "match_parent"
     android: orientation = "vertical"
      tools: context = "com.example.appdemo.MainActivity $ PlaceholderFragment"
      android: gravity = "center_horizontal"
        >
     <WebView
         android: layout_weight = "1"
         android: layout_width = "fill_parent"
         android: layout_height = "fill_parent"
         android: id = "@ + id / web_v"
         />
     <Button
         android: layout_width = "wrap_content"
         android: layout_height = "wrap_content"
         android: id = "@ + id / bt"
         android: text = "Get Password"
          android: layout_weight = "0"
          android: onClick = "getpassword"
         
         />
     </ LinearLayout>



(2) Then the page body is written in the following code, that is, the content of the Click event response, that is, the ID for content display password,<content> can be placed in any location of the Web page


<script>
function fillContent(){
	document.getElementById("content").innerHTML="隐藏密码为:1234567890"
}
</script>
<body>
<p id="content"></p>


(3) Add the following code to the activity's Click event





 public void getpassword(View v)
    {
    	web.loadUrl("javascript:fillContent()");
    }
}


Shown below: 1 after running, 2 is click on "Get password after page:"









How to use a Web page to develop your own app, a button in a Web page that binds to your own Java code to implement a call that JavaScript code calls Java code, and Java code to invoke JavaScript code


Related Article

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.