Html 按钮 OnClick 移动 Android 中的另一个 Activity
Html Button OnClick to move Another Activity in Android
我在 addJavascriptInterface 方法中遇到 None of the methods in the added interface @android.webkit.JavascriptInterface.They will not be visible in Api level 17.
编译错误。
我已经指出了下面代码中的错误行:
WebView代码:
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代码:
<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>
我正在使用 minSdkVersion 11 和 targetSdkVersion 22.I 在 method.You 可以在上面的代码中看到它之前调用了 @JavascriptInterface。
任何人都可以帮助我 this.Thank 你。
来自 Android 4.2 文档:
Caution: If you've set your targetSdkVersion to 17 or higher, you
must add the @JavascriptInterface annotation to any method that you
want available your web page code (the method must also be public). If
you do not provide the annotation, then the method will not accessible
by your web page when running on Android 4.2 or higher.
您必须在您的 class 中使用 @Javascript 接口注释每个您希望从 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 页数:
<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>
FirstACtivity.java:
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);
}
}
更多参考:检查this tutorial。
我在 addJavascriptInterface 方法中遇到 None of the methods in the added interface @android.webkit.JavascriptInterface.They will not be visible in Api level 17.
编译错误。
我已经指出了下面代码中的错误行:
WebView代码:
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代码:
<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>
我正在使用 minSdkVersion 11 和 targetSdkVersion 22.I 在 method.You 可以在上面的代码中看到它之前调用了 @JavascriptInterface。
任何人都可以帮助我 this.Thank 你。
来自 Android 4.2 文档:
Caution: If you've set your targetSdkVersion to 17 or higher, you must add the @JavascriptInterface annotation to any method that you want available your web page code (the method must also be public). If you do not provide the annotation, then the method will not accessible by your web page when running on Android 4.2 or higher.
您必须在您的 class 中使用 @Javascript 接口注释每个您希望从 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 页数:
<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>
FirstACtivity.java:
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);
}
}
更多参考:检查this tutorial。