如何从Web视图中调用对话框

如何从Web视图中调用对话框

本文介绍了如何从Web视图中调用对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个webview和一个类,该类的函数将与webview中的javascript一起使用

I have a webview and a class with functions that will be used with javascript inside the webview

具有函数(WebAppInterface.java)的类:

class with functions (WebAppInterface.java):

public class WebAppInterface {
    Context mContext;

    WebAppInterface(Context c) {
        mContext = c;
    }

    @JavascriptInterface
    public void closeApp() {
        ((Activity)mContext).finishAffinity();
    }

    @JavascriptInterface
    public void refresh() {
        Intent i = new Intent(mContext, MainActivity.class);
        mContext.startActivity(i);
        ((Activity)mContext).finish();
    }
}

如何在此文件内调用/创建Dialog类?

How can I call/create a Dialog class inside this file?

我试图打给:

Dialog ex = new Dialog();
ex.show(getSupportFragmentManager(), "Dialog");

但是它什么都不做

推荐答案

我希望您已经添加了活动:

I hope you have added in your activity:

WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");

WebAppInterface不应扩展活动:

WebAppInterface should not extend Activity:

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 showDialog(String text) {
    //here code of alert dialog
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
    builder.setTitle("abcd");
    builder.setMessage(text);
    builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
    Dialog dialog = builder.create();
    dialog.show();
}}

现在您在课堂上拥有活动上下文,因此您可以在课堂上执行所有其他任务

Now you have activity context in class so you can perform all other task in this class

这篇关于如何从Web视图中调用对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 22:46