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

编程入门 行业动态 更新时间:2024-10-09 10:21:18
本文介绍了如何从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视图中调用对话框?

本文发布于:2023-11-27 09:59:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637604.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   对话框   Web

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!