现在我在网页类中有一个嵌套的 webviewclient 类。
我覆盖了ShouldOverrideUrlLoading,并在我的 webview 中加载了新的 url。但在某些网址上,我只想关闭 webview。
我尝试在ShouldOverrideUrlLoading中添加它,但我无法在那里开始/完成活动。我也尝试在网页类中创建一个函数,但我未能从嵌套类中调用它。
现在我只是在按下返回时关闭 webview,但我不希望用户必须做那么多工作..
我使用 Xamarin(C#) 为 Android 开发,但 Java 答案很可能也有帮助!
[Activity (Label = "WebPage", Theme = "@android:style/Theme.NoTitleBar")]
public class WebPage : Activity
{
WebView web_view;
private class HelloWebViewClient : WebViewClient
{
public override bool ShouldOverrideUrlLoading (WebView view, string url)
{
view.LoadUrl (url);
return true;
}
}
public void dofinish()
{
var activity2 = new Intent (this, typeof(MainActivity));
activity2.PutExtra("targeturl", targeturl);
StartActivity(activity2);
Finish();
}
public string targeturl;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
string text = Intent.GetStringExtra ("MyData") ?? "Data not available";
targeturl = Intent.GetStringExtra ("targeturl") ?? "No Target Url";
SetContentView (Resource.Layout.WebView);
web_view = FindViewById<WebView> (Resource.Id.LocalWebview);
web_view.Settings.JavaScriptEnabled = true;
web_view.LoadUrl (text);
web_view.SetWebViewClient (new HelloWebViewClient ());
}
public override void OnBackPressed()
{
var activity2 = new Intent (this, typeof(MainActivity));
activity2.PutExtra("targeturl", targeturl);
StartActivity(activity2);
Finish();
}
}