gpt4 book ai didi

javascript - 在 xamarin Android CrossWalk Web View 中启用 javascript 警报对话框

转载 作者:行者123 更新时间:2023-12-03 00:22:57 33 4
gpt4 key购买 nike

java 脚本警报在 Crosswalk Web View 中不起作用

这是我尝试过的:

    protected override void OnXWalkReady()
{
fa = this;

var view = new RelativeLayout(this.BaseContext);
var mp = ViewGroup.LayoutParams.MatchParent;
xwv = new Org.Xwalk.Core.XWalkView(this.BaseContext, this);
view.AddView(xwv);
this.AddContentView(view, new ViewGroup.LayoutParams(mp, mp));
XWalkPreferences.SetValue("enable-javascript", true);
xwv.SetResourceClient(new XWalkResourceClient(xwv));
xwv.SetUIClient(new XWalkUIClient(xwv));
xwv.AddJavascriptInterface(new Foo(this, xwv), "Foo");
xwv.LoadUrl("file:///android_asset/index.html");
}

使用 Crosswalk 但不使用 Xamarin 时,JavaScript 警报在 Android Studio 中默认工作。

更新

class Foo : Java.Lang.Object
//, Java.Lang.IRunnable
{
Context context;
XWalkView xwv;
Activity activity;
public Foo(Context context, Org.Xwalk.Core.XWalkView xwv)
{
this.context = context;
this.activity = activity;
this.xwv = xwv;
}


[Org.Xwalk.Core.JavascriptInterface]
//[Export]
/// [JavascriptInterface]
[Java.Interop.Export("Run")]
public void Run(string text, string id)
{

Toast.MakeText (context, text,ToastLength.Long).Show();
}
}

html 索引文件:

   <body>
<div onclick="clicked()">Say Hello</div>
<script>
function clicked() {
alert();
Foo.Run('this test from js', "1");
}
</script>
<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
</body>

这是我在索引 HTML 文件中的 HTML 元素,我尝试显示警报并选择标记,但没有成功。

最佳答案

我测试了您的代码和java脚本警报在Crosswalk Web View 中不起作用,错误消息如下:“Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null无效;您的事件正在运行吗?"

您可以通过变通方法解决此问题:在 Foo() 中引发一个alertDialog,并在您想在 javascript 中使用警报时注入(inject)该方法。

我根据你发布的代码写了一个demo,你可以试试:

1 在 Foo 中导出一个Alert()方法来显示alertDialog

   protected override void OnXWalkReady()
{
var fa = this;

var view = new RelativeLayout(this.BaseContext);
var mp = ViewGroup.LayoutParams.MatchParent;
var xwv = new Org.Xwalk.Core.XWalkView(this.BaseContext, this);
view.AddView(xwv);
this.AddContentView(view, new ViewGroup.LayoutParams(mp, mp));
XWalkPreferences.SetValue("enable-javascript", true);
xwv.SetResourceClient(new XWalkResourceClient(xwv));
xwv.SetUIClient(new XWalkUIClient(xwv));
xwv.AddJavascriptInterface(new Foo(this, xwv), "Foo");
xwv.Load("file:///android_asset/index.html", null);
}
}

class Foo : Java.Lang.Object
//, Java.Lang.IRunnable
{
Context context;
XWalkView xwv;
Activity activity;
public Foo(Context context, Org.Xwalk.Core.XWalkView xwv)
{
this.context = context;
this.activity = activity;
this.xwv = xwv;
}


[Org.Xwalk.Core.JavascriptInterface]
//[Export]
/// [JavascriptInterface]
[Java.Interop.Export("Run")]
public void Run(string text, string id)
{
Toast.MakeText(context, text, ToastLength.Long).Show();
}

[Org.Xwalk.Core.JavascriptInterface]
//[Export]
/// [JavascriptInterface]
[Java.Interop.Export("Alert")]
public void Alert(string text)
{
AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.SetTitle("Alert Message");
alert.SetMessage(text);
alert.SetPositiveButton("OK", (senderAlert, args) =>
{
//Toast.MakeText(context, "OK!", ToastLength.Short).Show();
});

Dialog dialog = alert.Create();
dialog.Show();
}

2 每当你想在index.html中显示警报时调用Alert()方法,并传递一个字符串作为参数来设置你想要警报的消息。

index.html:

<body>
<div onclick="clicked()">Say Hello</div>
<script>
function clicked() {
Foo.Alert("This is alert message");
Foo.Run('this test from js', "1");
}
</script>
<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
</body>

关于javascript - 在 xamarin Android CrossWalk Web View 中启用 javascript 警报对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54226551/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com