我创建了一个使用webview访问HTML文件的android应用程序。这样做是为了使用JavaScriptInterface将2个javascript函数映射到2个本机android函数。这两个功能调出并解除了android软键盘。
当我最初测试该文件时,我在本地运行它并且它有效。从那时起,我将文件放在服务器上并更改了webview以通过服务器访问HTML文件。由于将它放在服务器上,因此不再调用javascript接口函数。
是否需要设置标志或我缺少的权限?我搜索无济于事。
这是HTML代码
<head id="Head1" runat="server">
<title>Login Page</title>
<script language="javascript" type="text/javascript">
function capturePassword(event)
{
event = (event) ? event : window.event
var charCode = (event.which) ? event.which : event.keyCode
if (charCode == 13) {
document.getElementById("password").focus();
document.getElementById("password").click();
}
}
function myFunction()
{
jsi.showKeyboard(); //this is a function in the webview android app which does not get called when the file is placed on the server
}
function hideKeyboardMaybe()
{
event = (event) ? event : window.event
var charCode = (event.which) ? event.which : event.keyCode
if (charCode == 13) {
jsi.hideKeyboard(); //same here
}
}
</script>
</head>
<body>
User name: <input type="number" id="fname" autofocus onkeyup="capturePassword(event)"><br>
Password: <input type="number" id="password" onfocus="myFunction()" onkeyup="hideKeyboardMaybe(event)"><br>
</body>
</html>
这是webview代码
public class MainActivity extends Activity {
@SuppressLint("JavascriptInterface")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final WebView mWebView=(WebView)findViewById(R.id.webView1);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setSaveFormData(true);
mWebView.getSettings().setBuiltInZoomControls(true);
JavaScriptInterface jsi = new JavaScriptInterface(this, mWebView);
mWebView.addJavascriptInterface(jsi, "jsi");
//setContentView(mWebView);
mWebView.loadUrl("file://"+ Environment.getExternalStorageDirectory() + "/sippopup.html");
mWebView.requestFocus(View.FOCUS_DOWN);
}
public class JavaScriptInterface {
Context mContext;
WebView v;
/** Instantiate the interface and set the context */
JavaScriptInterface(Context c, WebView v) {
mContext = c;
this.v=v;
}
@JavascriptInterface
public void showKeyboard()
{
InputMethodManager inputMethodManager= (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(v, 0);
}
@JavascriptInterface
public void hideKeyboard()
{
InputMethodManager inputMethodManager=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);
}
}
}
答案 0 :(得分:0)
默认情况下,在WebViews中禁用JavaScript,您需要在代码中启用它。
WebView myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);