Interaction between Android and JavaScrip (1), android+crip

Source: Internet
Author: User

Interaction between Android and JavaScrip (1), android+crip

I. JS code is as follows:

Demo2.xml

<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE html PUBLIC "-// WAPFORUM // dtd xhtml Mobile 1.0 // EN" http://www.wapforum.org/DTD/xhtml-mobile10.dtd "> 
<pre name="code" class="html"><span style="white-space:pre"></span>onClick="window.demo.clickButton()"<span style="font-family: Arial, Helvetica, sans-serif;"></span>
OnClick = "test2 ()"/> <input onClick = "test () "type =" button "id =" testbtn2 "value =" I am button 2 "/> </body> <script language =" javascript "> function test () {alert ("click button 2");} function test2 () {alert ("click button 1");} function wave () {alert ("click the button in andorid") ;}</script>

The above button 1 sets two Click Event methods, one is the method in android, and the other is the method in JS, but the click event only responds once. 

Put the android method in front. Open the html file in the browser and click button 1. The method in andoid is located in the front of the response event. The method is not found in the browser, so no response is returned, the second click event is the method in JS and can be executed, so it is responded. When running in the app, the first method found is the executable method, so it is responded. The Click Event method can be executed, but the click event has responded, so it is not executed.

Ii. Functions

1. click the button in andorid to respond to the method in JS.

2. click the button in JS to respond to the method in Android

Main. xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <Button        android:id="@+id/mCallJS"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="CallJs" />    <WebView        android:id="@+id/webview"        android:layout_width="fill_parent"        android:layout_height="fill_parent" /></LinearLayout>

MethodMutual2.java

Package com. example. mutal; import android. app. activity; import android. app. alertDialog; import android. content. dialogInterface; import android. OS. bundle; import android. OS. handler; import android. util. log; import android. view. view; import android. view. view. onClickListener; import android. webkit. jsResult; import android. webkit. webChromeClient; import android. webkit. webSettings; import android. webkit. webView; import android. widget. button; import android. widget. toast; public class methodmutual extends Activity {private WebView mWebView; private static final String LOG_TAG = "WebViewDemo";/** Called when the activity is first created. * // @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); loadAssetHtml ();} public void loadAssetHtml () {mWebView = (WebView) findViewById (R. id. webview); WebSettings webSettings = mWebView. getSettings (); webSettings. setSavePassword (false); webSettings. setSaveFormData (false); webSettings. setJavaScriptEnabled (true); webSettings. setsuppzoom zoom (false); mWebView. setWebChromeClient (new MyWebChromeClient (); // binds a java object to a javascript Object. The javascript Object Name Is interfaceName and the scope is Global. mWebView. addJavascriptInterface (new DemoJavaScriptInterface (), "demo"); mWebView. loadUrl ("file: // android_asset/demo2.html"); // click the trigger JS function response Button mCallJS = (Button) findViewById (R. id. mCallJS); mCallJS. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {mWebView. loadUrl ("javascript: wave ()") ;}}) ;}private int I = 0; final class DemoJavaScriptInterface {DemoJavaScriptInterface () {}/*** This is not called on the UI thread. post a runnable to invoke * loadUrl on the UI thread. */public void clickButton () {new AlertDialog. builder (MethodMutual2.this ). setMessage ("the button you clicked is 2 "). create (). show () ;}/ *** Provides a hook for calling "alert" from javascript. useful for debugging * your javascript. */final class MyWebChromeClient extends WebChromeClient {@ Overridepublic boolean onJsAlert (WebView view, String url, String message, JsResult result) {// handle the self-blocking dialog box // Toast. makeText (getApplicationContext (), message, 0 ). show (); // result. confirm (); // return true; // use the js dialog box return super. onJsAlert (view, url, message, result );}}}
Analysis:

Click the android button to respond to the wave () method in JS.

Click button 1 -- Response to the clickButton () method in the android app; response to the test2 () method in JS in the browser

Click button 2-respond to the test () method in JS


Android programming interface Interaction

It is mainly used for front-end display. Girls enjoy a better aesthetic and the interface is generally better than boys! Generally, various la s are used, and the class used is XX Activity! If you do not play games, the normal View class will be OK. For example, ListView and Button may rewrite some methods. SurfaceView is used to save memory. I don't know much about games. Code logic, such as reading json data and xml from the Internet.

How do I remember the logon status when android interacts with javaweb?

Use this: SharedPreference
Here is an example.

Import assets. webkit .*;
Import android. R. raw;

Import android. app. Activity;
Import android. content. Intent;
Import android. content. SharedPreferences;
Import android. OS. Bundle;
Import android. text. Editable;
Import android. text. InputType;
Import android. text. TextWatcher;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. ArrayAdapter;
Import android. widget. AutoCompleteTextView;
Import android. widget. Button;
Import android. widget. CheckBox;
Import android. widget. EditText;
Import android. widget. Toast;

Public class LoginActivity extends Activity {
AutoCompleteTextView cardNumAuto;
EditText passwordET;
Button logBT;
CheckBox savePasswordCB;
SharedPreferences sp;
String cardNumStr;
String passwordStr;

/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. login );
CardNumAuto = (AutoCompleteTextView) findViewById (R. id. cardNumAuto );

PasswordET = (EditText) findViewById (R. id. passwordET );

LogBT = (Button) findViewById (R. id. logBT );

Sp = this. getSharedPreferences ("passwordFile", MODE_PRIVATE );

SavePasswordCB = (CheckBox) findViewById (R. id. savePasswordCB );

SavePasswordCB. setChecked (true); // remember the password by default.

CardNumAuto. setThreshold (1); // enter 1 letter to start automatic prompt

PasswordET. setInputType (InputType. TYPE_CLASS_TEXT

| InputType. TYPE_TEXT_VARIATION_PASSWORD );

// Hide the password as InputType. TYPE_TEXT... the remaining full text>

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.