我在webview中有两个按钮。我想给他们手动点击事件。 下面是我的代码;
网页代码
<form method="post" name="refer_now">
<div class="refer-now-button">
<!-- <button type="submit" class="refer-now-btn" id="refer-now">refer now</button> -->
<input type="button" class="refer-now-btn" value="refer now" name="refernow" id="refernow" onClick="showAndroidToast('refer');" />
</div>
</form>
<form method="post" name="refer_now_footer">
<div class="refer-now-button">
<!-- <button type="submit" class="refer-now-btn" id="refer_now_footer">refer now</button> -->
<input type="button" class="refer-now-btn" value="refer now" name="refer-footer" id="refer-footer" onClick="showAndroidToast('bottom_refer');" />
</div>
</form>
下面是我的Java代码;
mWebview.loadUrl(link);
mWebview.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
//Required functionality here
return super.onJsAlert(view, url, message, result);
}});
mWebview.addJavascriptInterface(new WebAppInterface(this), "AndroidInterface");
mWebview.getSettings().setJavaScriptEnabled(true);
mWebview.getSettings().setLoadsImagesAutomatically(true);
mWebview.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
public class WebAppInterface {
Context mContext;
// Instantiate the interface and set the context
WebAppInterface(Context c) {
mContext = c;
}
@JavascriptInterface
public void showevent(String toast) {
if(toast.equalsIgnoreCase("refer")){
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}else if(toast.equalsIgnoreCase("bottom_refer")){
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
}
}
当我运行这段代码时;这给了我下面的错误:
“未捕获的ReferenceError:未定义AndroidInterface”,
答案 0 :(得分:1)
要从android中的webview执行按钮单击,请执行以下操作:
HTML和Java脚本
<!DOCTYPE html>
<html lang="en">
<body>
<input type="button" value="Say hello" onClick="showAndroidToast('OK')" />
<input type="button" value="hello mr.vicky" onClick="showAndroidToast('MR.VICKY')" />
<br/><br/>
<script type="text/javascript">
<!-- Sending value to Android -->
function showAndroidToast(toast) {
AndroidInterface.showToast(toast);
}
</script>
</body>
</html>
JAVA代码
package com.ellvar.Activity.Common.Activity;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.JavascriptInterface;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.Toast;
import com.ellvar.CustomView.CustomDialog;
import com.ellvar.CustomView.CustomProgressDialog;
import com.ellvar.R;
import com.ellvar.Utils.Utility;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class TermsConditionActivitydemo extends AppCompatActivity {
@BindView(R.id.wv_termscondition)
WebView wvTermscondition;
@BindView(R.id.iv_back)
ImageView ivBack;
CustomProgressDialog progressDialog;
@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_terms_condition);
ButterKnife.bind(this);
if (!Utility.getInstance().isNetworkAvailable(this)) {
CustomDialog.getInstance().showalert(this, getString(R.string.please_check_internet_connection));
}else{
loaddata();
}
}
private void loaddata() {
wvTermscondition.loadUrl("file:///android_asset/index.html");
wvTermscondition.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
//Required functionality here
return super.onJsAlert(view, url, message, result);
}});
wvTermscondition.addJavascriptInterface(new WebAppInterface(this), "AndroidInterface");
wvTermscondition.getSettings().setJavaScriptEnabled(true);
wvTermscondition.getSettings().setLoadsImagesAutomatically(true);
wvTermscondition.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
}
@OnClick(R.id.iv_back)
public void onViewClicked() {
finish();
}
public class WebAppInterface {
Context mContext;
// Instantiate the interface and set the context
WebAppInterface(Context c) {
mContext = c;
}
@JavascriptInterface
public void showToast(String toast) {
if(toast.equalsIgnoreCase("OK")){
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}else if(toast.equalsIgnoreCase("MR.VICKY")){
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
}
}
}
输出
答案 1 :(得分:0)
我不确定是否可以使用不同的addJavaScriptInterface
多次调用names
方法。尝试将您的performClick1
方法移到相同的JavaScriptInterface
下,例如:
view.addJavascriptInterface(new Object() {
@JavascriptInterface
public void performClick() {
Intent i = new Intent(FollowPageActivity.this, MyLoyaltyActivity.class);
startActivity(i);
}
@JavascriptInterface
public void performClick1() {
Intent i = new Intent(FollowPageActivity.this, MyLoyaltyActivity.class);
startActivity(i);
}
}, "refer_now");// identify which button you click
答案 2 :(得分:0)
名称为refer_now
的表单将分配给全局对象window
的相同属性。
这意味着refer_now
中的onclick="refer_now.performClick();"
是表单元素。
密钥是确保form
和javascriptinterface
的名称不同。
这是一个解决方案:保留Java代码并使用以下内容更改html:
<form method="post" name="refer_now_form">
<div class="refer-now-button">
<!-- <button type="submit" class="refer-now-btn" id="refer-now">refer now</button> -->
<input type="submit" class="refer-now-btn" value="refer now" name="refernow" id="refernow" onclick="refer_now.performClick();" />
</div>
</form>
<form method="post" name="refer_now_footer_form">
<div class="refer-now-button">
<!-- <button type="submit" class="refer-now-btn" id="refer_now_footer">refer now</button> -->
<input type="submit" class="refer-now-btn" value="refer now" name="refer-footer" id="refer-footer" onclick="refer_now_footer.performClick1();" />
</div>
</form>