单击Html按钮可在Android中移动另一个活动
我在added interface@android.webkit.JavascriptInterface中没有得到单击Html按钮可在Android中移动另一个活动,android,android-webview,Android,Android Webview,我在added interface@android.webkit.JavascriptInterface中没有得到任何方法。它们在Api级别17中不可见。addJavascriptInterface方法的编译错误 我已经指出了下面代码中的错误行: 网络视图代码: import android.webkit.JavascriptInterface; webView = (WebView) findViewById(R.id.load_url); webView.getSettings().set
任何方法。它们在Api级别17中不可见。
addJavascriptInterface方法的编译错误
我已经指出了下面代码中的错误行:
网络视图代码:
import android.webkit.JavascriptInterface;
webView = (WebView) findViewById(R.id.load_url);
webView.getSettings().setJavaScriptEnabled(true);
if (new File(url).exists()) {
webView.loadUrl(FILENAME_PREFIX + url);
Log.d("fileurl", "" + FILENAME_PREFIX + url);
}
webView.addJavascriptInterface(new Object() { --->None of the methods in the added interface @android.webkit.JavascriptInterface.They will not be visible in Api level 17.
@JavascriptInterface
public void performClick() {
Intent intRef=new Intent(FirstActivity.this,SecondActivity.class);
startActivity(intRef);
}
}, "ok");
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<div>
<button type="button" onclick="ok.performClick();">OK</button>
</div>
</body>
</html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="style.css" />
<script type="text/javascript">
function moveToScreenTwo() {
Android.moveToNextScreen();
}
</script>
</head>
<body>
<div>
<input type="button" value="Locate" onClick="moveToScreenTwo()" />
</div>
</body>
</html>
import android.webkit.JavascriptInterface;
webView = (WebView) findViewById(R.id.load_url);
webView.getSettings().setJavaScriptEnabled(true);
if (new File(url).exists()) {
webView.loadUrl(FILENAME_PREFIX + url);
Log.d("fileurl", "" + FILENAME_PREFIX + url);
webView.addJavascriptInterface(new WebAppInterface(this), "Android");
}
//Class to be injected in Web page
public class WebAppInterface {
Context mContext;
/**
* Instantiate the interface and set the context
*/
WebAppInterface(Context c) {
mContext = c;
}
@JavascriptInterface
public void moveToNextScreen() {
Intent i = new Intent(FirstActivity.this,SecondActivity.class);
startActivity(i);
}
}
Html代码:
import android.webkit.JavascriptInterface;
webView = (WebView) findViewById(R.id.load_url);
webView.getSettings().setJavaScriptEnabled(true);
if (new File(url).exists()) {
webView.loadUrl(FILENAME_PREFIX + url);
Log.d("fileurl", "" + FILENAME_PREFIX + url);
}
webView.addJavascriptInterface(new Object() { --->None of the methods in the added interface @android.webkit.JavascriptInterface.They will not be visible in Api level 17.
@JavascriptInterface
public void performClick() {
Intent intRef=new Intent(FirstActivity.this,SecondActivity.class);
startActivity(intRef);
}
}, "ok");
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<div>
<button type="button" onclick="ok.performClick();">OK</button>
</div>
</body>
</html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="style.css" />
<script type="text/javascript">
function moveToScreenTwo() {
Android.moveToNextScreen();
}
</script>
</head>
<body>
<div>
<input type="button" value="Locate" onClick="moveToScreenTwo()" />
</div>
</body>
</html>
import android.webkit.JavascriptInterface;
webView = (WebView) findViewById(R.id.load_url);
webView.getSettings().setJavaScriptEnabled(true);
if (new File(url).exists()) {
webView.loadUrl(FILENAME_PREFIX + url);
Log.d("fileurl", "" + FILENAME_PREFIX + url);
webView.addJavascriptInterface(new WebAppInterface(this), "Android");
}
//Class to be injected in Web page
public class WebAppInterface {
Context mContext;
/**
* Instantiate the interface and set the context
*/
WebAppInterface(Context c) {
mContext = c;
}
@JavascriptInterface
public void moveToNextScreen() {
Intent i = new Intent(FirstActivity.this,SecondActivity.class);
startActivity(i);
}
}
好啊
我使用的是MinsdkVersion11和TargetSDkVersion22。我在before方法中调用了@JavascriptInterface。您可以在上面的代码中看到这一点
任何人都可以帮我。谢谢。来自Android 4.2文档: 警告:如果您已将targetSdkVersion设置为17或更高,则 必须将@JavascriptInterface注释添加到您选择的任何方法中 希望您的网页代码可用(该方法也必须是公共的)。如果 如果不提供注释,则无法访问该方法 在Android 4.2或更高版本上运行时,通过您的网页 您必须在类中使用@JavascriptInterface注释您希望从Javascript访问的每个方法 编辑: 我在代码中实现了这种方式:
webView.addJavascriptInterface(new WebAppInterface(this), "ok");
public class WebAppInterface {
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}
@JavascriptInterface
public void performClick() {
Intent intRef=new Intent(FirstActivity.this,SecondActivity.class);
startActivity(intRef);
}
}
我希望有帮助 Html页面:
import android.webkit.JavascriptInterface;
webView = (WebView) findViewById(R.id.load_url);
webView.getSettings().setJavaScriptEnabled(true);
if (new File(url).exists()) {
webView.loadUrl(FILENAME_PREFIX + url);
Log.d("fileurl", "" + FILENAME_PREFIX + url);
}
webView.addJavascriptInterface(new Object() { --->None of the methods in the added interface @android.webkit.JavascriptInterface.They will not be visible in Api level 17.
@JavascriptInterface
public void performClick() {
Intent intRef=new Intent(FirstActivity.this,SecondActivity.class);
startActivity(intRef);
}
}, "ok");
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<div>
<button type="button" onclick="ok.performClick();">OK</button>
</div>
</body>
</html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="style.css" />
<script type="text/javascript">
function moveToScreenTwo() {
Android.moveToNextScreen();
}
</script>
</head>
<body>
<div>
<input type="button" value="Locate" onClick="moveToScreenTwo()" />
</div>
</body>
</html>
import android.webkit.JavascriptInterface;
webView = (WebView) findViewById(R.id.load_url);
webView.getSettings().setJavaScriptEnabled(true);
if (new File(url).exists()) {
webView.loadUrl(FILENAME_PREFIX + url);
Log.d("fileurl", "" + FILENAME_PREFIX + url);
webView.addJavascriptInterface(new WebAppInterface(this), "Android");
}
//Class to be injected in Web page
public class WebAppInterface {
Context mContext;
/**
* Instantiate the interface and set the context
*/
WebAppInterface(Context c) {
mContext = c;
}
@JavascriptInterface
public void moveToNextScreen() {
Intent i = new Intent(FirstActivity.this,SecondActivity.class);
startActivity(i);
}
}
更多参考:检查。参考此-阅读此内容,它只适用于我指出的一种方法。