首页 > 解决方案 > 关闭 ajax 函数成功事件的 webview

问题描述

我想关闭我的 webview 或重定向到让我们说MainActivity.Java当我从 Ajax 函数获得成功事件时。

让我解释..

在按钮的单击事件上,我WebviewActivity.java通过意图调用它。现在我有我通过加载到 webview 的 php 文件webview.loadUrl()。现在用户将进入此页面并输入他们的详细信息,当按下确定时,他们的数据将通过 php 文件的 ajax 调用插入到页面中。在成功的 ajax 调用时,该div<div>标签将被隐藏,另一个<div>标签将显示其中包含一条消息“你详情已记录。” 现在我已经把onBackPress()用户重新打开了MainActivity.java。但不是那样,我想要在成功的事件 webview 自动关闭或自动重定向到之后MainActivity.java。有可能做那种事情吗?任何帮助将不胜感激。提前致谢。

这是webview的代码。

public class WebViewActivity extends AppCompatActivity {

Toolbar urTollbar;
WebView webView;
String parsringUrl;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_web_view);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    urTollbar = (Toolbar)findViewById(R.id.URanim_toolbar);
    setSupportActionBar(urTollbar);
    if (getSupportActionBar() != null) {
        getSupportActionBar().setTitle("Submit For Approval");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    webView = (WebView) findViewById(R.id.webview);
    parsringUrl =  getIntent().getStringExtra("ParsingURL");
    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webView.loadUrl(parsringUrl);




}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if (item.getItemId() == android.R.id.home) {
        finish();
        overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);

    }

    return super.onOptionsItemSelected(item);

}

@Override
public void onBackPressed() {
    super.onBackPressed();
    overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
    Intent intent = new Intent(WebViewActivity.this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    startActivity(intent);
}

}

标签: javascriptandroidwebview

解决方案


您可以在 webview 中从 javascript 调用 java 函数。

爪哇

Java 方法通过一个特殊的类暴露给 Javascript。这是一个例子。公开的方法使用@JavascriptInterface 注释进行注释。文档说注释对于 API 级别 17 及更高级别是强制性的。我不确定,但我认为在 SDK 17 之前所有方法都已公开。

public class WebAppInterface {
    Context mContext;

    /** Instantiate the interface and set the context */
    WebAppInterface(Context c) {
        mContext = c;
    }

    /** Show a toast from the web page */
    @JavascriptInterface
    public void showToast(String toast) {
        Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
    }
}

将类添加到 webview

myWebView.addJavascriptInterface(new WebAppInterface(this), "android");

第一个参数是带有上下文的接口类,后者是要在 Javascript 中使用的变量名。

Javascript

<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />

<script type="text/javascript">
    function showAndroidToast(toast) {
        if(typeof android !== "undefined" && android !== null) {
            android.showToast(toast);
        } else {
            alert("Not viewing in webview");
        }
    }
</script>

在 Javascript 中有一个名为 Android 的全局对象。对象名称来自 addJavascriptInterface 方法。该对象具有接口类中注释的所有方法,在此示例中为 showToast(String toast)。if 语句用于检查对象是否已设置。有了这个 if 语句,该站点也可以在其他浏览器中使用,也可以在不存在 Android 界面的情况下使用。


推荐阅读