Android/webview/js有効
提供: 初心者エンジニアの簡易メモ
MainActivity.java
import android.os.Build; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); WebView webview = new WebView(this); webview = (WebView)this.findViewById(R.id.webview1); webview.setWebViewClient(new WebViewClient()); WebSettings setting = webview.getSettings(); setting.setJavaScriptEnabled(true); webview.loadUrl("file:///android_asset/index.html"); } }
- src/main/res/layout/activity_main.xml
<?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_parent" > <WebView android:id="@+id/webview1" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> </android.support.constraint.ConstraintLayout>
- src/main/assets/index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <script language="javascript"> function sum() { document.forms.form1.text1.value = 1 + 2; } </script> </head> <body> <form id="form1"> <input type="button" onclick="sum()" value="sum"/> <input id="text1" type="text"/> </form> </body> </html>