我在Android Studio上使用WebView处理了一个应用程序,并且用一个按钮来打开和关闭一个带有HTML,CSS和JavaScript的窗口(div)。问题是:按钮功能仅适用于Android 8.0版本(Genymotion模拟器),我不知道为什么会发生这种情况,因为其他一些功能正在正常工作。
一些示例代码:
HTML
按钮:
<button onclick="open_window()" class="button_template">ABC</div>
打开DIV:
<div class="window_template" id="window_1">12345</div>
CSS样式:
.window_template{
background-image: url(sample.jpg);
opacity: 0.9;
border: 2px solid black;
text-align: center;
margin: 0 auto;
box-sizing: border-box;
}
.button_template {
color: white;
font-family: Arial;
font-size: 105%;
background-color: transparent;
border: none;
cursor: pointer;
}
window_1{
width: 80%;
height: 35%;
position: absolute;
display: none;
}
JavaScript:
function open_window(){
document.getElementById("window_1").style.display="block";
}
主要活动:
package sample.sampla;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
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);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN );
setContentView(R.layout.activity_main);
WebView myWebView = (WebView)findViewById(R.id.webView1);
WebSettings mySettings = myWebView.getSettings();
myWebView.setWebChromeClient(new WebChromeClient());
myWebView.setWebViewClient(new WebViewClient());
mySettings.setDomStorageEnabled(true);
mySettings.setJavaScriptEnabled(true);
myWebView.loadUrl("file:///android_asset/www/index.html");
}
}
毕竟DIV没有打开。