标签:password 开放 nload .json 解析 sage size star ble
<?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:orientation="vertical" > <WebView android:id="@+id/webView" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
package com.yld.jsinteraction; import android.support.v7.app.ActionBarActivity; import android.webkit.WebSettings; import android.webkit.WebView; import android.annotation.SuppressLint; import android.os.Bundle; public class MainActivity extends ActionBarActivity { private WebView webView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.webView = (WebView) this.findViewById(R.id.webView); this.initializeWebView(); } @SuppressLint({ "NewApi", "SetJavaScriptEnabled" }) private void initializeWebView(){ webView.addJavascriptInterface(new JsOperator(MainActivity.this), "JsInteraction"); try { String url = "file:///android_asset/LoginJs/login.html"; WebSettings webSettings = webView.getSettings(); webSettings.setJavaScriptEnabled(true); webSettings.setAllowFileAccess(true); webSettings.setAllowFileAccessFromFileURLs(true); this.webView.loadUrl(url); } catch (Exception e) { e.printStackTrace(); } } }
package com.yld.jsinteraction; import org.json.JSONObject; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.content.Context; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.webkit.JavascriptInterface; public class JsOperator { private Context context; public JsOperator(Context context) { this.context = context; } /** * 弹出消息对话框 */ @JavascriptInterface public void showDialog(String message) { AlertDialog.Builder builder = new Builder(context); builder.setMessage(message); builder.setTitle("提示"); builder.setPositiveButton("确认", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); builder.setNegativeButton("取消", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.create().show(); } /** * 获取登录的username和password * @return JSON格式的字符串 */ @JavascriptInterface public String getLoginInfo(){ try{ JSONObject login = new JSONObject(); login.put("Username", "YLD"); login.put("Password", "111"); return login.toString(); }catch(Exception e){ e.printStackTrace(); } return null; } }
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title id="title">Login</title> <script type="text/javascript" src="login.js"></script> </head> <body style="background:lightblue"> <div style="margin-top: 20px;margin-left: 20px"> <div> <label>Username:</label> <input id="txtUsername" type="text" style="margin-left: 20px"/> </div> <div style="margin-top: 20px"> <label>Password:</label> <input id="txtPassword" type="text" style="margin-left: 20px"/> </div> <div style="margin-top: 20px;margin-left: 160px"> <button onclick="loginObj.login()" style="width:100px">Login</button> </div> </div> </body> </html>
var Login = (function(){ function Login(){ } Login.prototype.login = function(){ JsInteraction.showDialog("Login start..."); } Login.prototype.setLoginInfo = function(){ var logininfoJson = JsInteraction.getLoginInfo(); //解析json字符串 var logininfo = eval("("+logininfoJson+")"); document.getElementById("txtUsername").value = logininfo.Username; document.getElementById("txtPassword").value = logininfo.Password; } return Login; })(); var loginObj = new Login(); window.onload=function(){ loginObj.setLoginInfo(); }
Android WebView与JavaScript交互实现Web App
标签:password 开放 nload .json 解析 sage size star ble
原文地址:http://www.cnblogs.com/jhcelue/p/7266939.html