Android Studio - 如何使用 Webview 从本地路径加载 HTML5 内容?

标签 android html webview local-storage

在官方 Android 开发者页面中有这个主题在 WebView 中构建 Web 应用程序 http://developer.android.com/guide/webapps/webview.html

如果内容将从存储在 src 文件夹中的本地 html 文件夹加载,我想知道如何进行

我找不到任何可下载的代码示例或示例。

我用的是android studio

最佳答案

从 Android Activity 调用 WebView 内的 JavaScript。

http://android-er.blogspot.com/2011/10/call-javascript-inside-webview-from.html

非常好!

/assets/mypage.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width; user-scalable=0;" />
<title>My HTML</title>
</head>
<body>
<h1>MyHTML</h1>
<p id="mytext">Hello!</p>
<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />
<input type="button" value="Open Dialog" onClick="openAndroidDialog()" />
<script language="javascript">
   function showAndroidToast(toast) {
       AndroidFunction.showToast(toast);
   }

   function openAndroidDialog() {
       AndroidFunction.openAndroidDialog();
   }

   function callFromActivity(msg){
 document.getElementById("mytext").innerHTML = msg;
   }
</script>

</body>
</html>

主.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   >
<TextView 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="@string/hello"
   />
<EditText
   android:id="@+id/msg"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content" />
<Button
 android:id="@+id/sendmsg"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="Msg to JavaScript"
   />
<WebView
   android:id="@+id/mybrowser"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
  />
</LinearLayout>

主要代码:

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class AndroidHTMLActivity extends Activity {

 WebView myBrowser;
 EditText Msg;
 Button btnSendMsg;

 /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       myBrowser = (WebView)findViewById(R.id.mybrowser);

       final MyJavaScriptInterface myJavaScriptInterface
        = new MyJavaScriptInterface(this);
       myBrowser.addJavascriptInterface(myJavaScriptInterface, "AndroidFunction");

       myBrowser.getSettings().setJavaScriptEnabled(true); 
       myBrowser.loadUrl("file:///android_asset/mypage.html");

       Msg = (EditText)findViewById(R.id.msg);
    btnSendMsg = (Button)findViewById(R.id.sendmsg);
    btnSendMsg.setOnClickListener(new Button.OnClickListener(){

   @Override
   public void onClick(View arg0) {
    // TODO Auto-generated method stub
    String msgToSend = Msg.getText().toString();
    myBrowser.loadUrl("javascript:callFromActivity(\""+msgToSend+"\")");

   }});

   }

 public class MyJavaScriptInterface {
  Context mContext;

     MyJavaScriptInterface(Context c) {
         mContext = c;
     }

     public void showToast(String toast){
         Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
     }

     public void openAndroidDialog(){
      AlertDialog.Builder myDialog
      = new AlertDialog.Builder(AndroidHTMLActivity.this);
      myDialog.setTitle("DANGER!");
      myDialog.setMessage("You can do what you want!");
      myDialog.setPositiveButton("ON", null);
      myDialog.show();
     }

 }
}

关于Android Studio - 如何使用 Webview 从本地路径加载 HTML5 内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18602676/

相关文章:

android - float 操作按钮行为随机

python - 使用 XPath 在使用 python 的 <td> 单元格中获取文本

html - 相对于设备大小滚动

javascript - 在 Android webview 上,当我执行 javascript 时,sotfkeyboard 消失了

android - 使用 React Native WebView 访问 localhost:9000/login 时出现 net::ERR_CONNECTION_REFUSED

JavaFX Web View : how do I change the default cursor?

android - 媒体编解码器 dequeueOutputBuffer IllegalStateException

java - 记忆一个 Activity

Android:通过 Intent 启动时相机方向

javascript - Bootstrap 下拉效果 - jQuery