1.
<! DOCTYPE html> 2.
public class Mainactivity extends Activity {WebView WebView; Button Buttonleft, Buttonright; EditText EditText; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); WebView = Findviewbyid (R.id.webview); Buttonleft = Findviewbyid (r.id.btnleft); Buttonright = Findviewbyid (r.id.btnright); EditText = Findviewbyid (R.id.edittext); WebSettings websettings = Webview.getsettings (); Allows the use of JS websettings.setjavascriptenabled (TRUE); Set Allow JS pop-up window websettings.setjavascriptcanopenwindowsautomatically (true); Webview.loadurl ("file:///android_asset/index.html"); Buttonleft.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (view view) {Webview.post (new Runnable () {@Override public void run () { Webview.evaluatejavascript ("Javascript:calljs ()", New Valuecallback<string> () {@Overr IDE public void Onreceivevalue (String s) {//change the text displayed by the button to JS returned String Buttonleft.settext (s); } }); } }); } }); Webview.setwebviewclient (New Webviewclient () {@Override public boolean shouldoverrideurlloading (Web View view, String URL) {//1. According to the parameters of the protocol, determine whether the required URL//is generally based on scheme (protocol format) & Authority (protocol name) Broken (first two parameters)//assuming the incoming URL = "js://webview?arg1=111&arg2=222" (also a well-agreed-upon) URI URI = URI.P Arse (URL); If the protocol of the URL = pre-agreed JS protocol//Parse down the parse parameter if (Uri.getscheme (). Equals ("JS")) { If authority = WebView in a pre-agreed agreement, the representation isConforming protocol//So intercept URL, below JS start call Android Required method if (uri.getauthority (). Equals ("WebView")) {//The logic Edittext.settext required to execute JS (uri.getqueryparameter ("Ar G1 ")); } return true; } return super.shouldoverrideurlloading (view, URL); } }); Webview.setwebchromeclient (New Webchromeclient () {@Override public boolean onjsprompt (WebView view, String URL, String message, string defaultvalue, jspromptresult result) {//Block input box//parameter Mes Sage: Content representing PROMT () (not URL)//Parameter result: Represents the return value of the input box//According to the parameters of the protocol, determine whether the desired URL//general root According to scheme (protocol format) & Authority (protocol name) (first two parameters)//assuming incoming URL = "js://webview?arg1=111&arg2=222" (also agreed intercepted) URI uri = uri.parse (URL); if (Uri.getscheme (). Equals ("JS")) {if (uri.getauthority () Equals ("demo")); {result.confirm ("JS has called the Android Method successfully"); return true; }} return Super.onjsprompt (view, URL, message, defaultvalue, result); } @Override public Boolean onjsalert (WebView view, string URL, String message, final Jsresult result ) {Alertdialog.builder b = new Alertdialog.builder (mainactivity.this); B.settitle ("Alert1"); B.setmessage (message); B.setpositivebutton (Android. R.string.ok, New Dialoginterface.onclicklistener () {@Override public void OnClick (D Ialoginterface dialoginterface, int i) {result.confirm (); } }); B.setcancelable (FALSE); B.create (). Show (); return true; } }); }} 3
<?xml version= "1.0" encoding= "Utf-8"? ><android.support.constraint.constraintlayout xmlns:android= "http:/ /schemas.android.com/apk/res/android "xmlns:app=" Http://schemas.android.com/apk/res-auto "xmlns:tools="/HTTP/ Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Tools:context =". Mainactivity "> <linearlayout android:layout_width=" match_parent "android:layout_height=" Match_paren T "tools:layout_editor_absolutex=" 136DP "tools:layout_editor_absolutey=" 0DP "android:orientation=" ver Tical "tools:ignore=" missingconstraints "> <webview android:id=" @+id/webview "and Roid:layout_width= "Match_parent" android:layout_height= "Match_parent" android:layout_weight= "1" > </WebView> <linearlayout android:layout_width= "Match_parent" Android:layout_h Eight= "Match_parent" Androidoid:orientation= "Horizontal" android:layout_weight= "3" > <button android:id= "@+i D/btnleft "android:layout_weight=" 1 "android:layout_width=" Match_parent "Android oid:layout_height= "Match_parent" android:text= "left"/> <edittext android:id= "@+id/edittext" android:layout_width= "match_parent" android:layout_height= "Match_parent" android:layout_weight= "1" android:gravity= "center" android:text= "0"/> <button android:id= "@+id/btnright" android:layout_weight= "1" android:l Ayout_width= "Match_parent" android:layout_height= "match_parent" android:text= "right"/> </LinearLayout> </LinearLayout></android.support.constraint.ConstraintLayout>
Android WebView JS Transfer value prompt