Хотите ли вы получить результат, как следующий GIF?
Если вы хотите go вернуться на предыдущую страницу. Вы можете использовать webView.GoBack();
private void BtnBack_Click(object sender, EventArgs e)
{
if (webView.CanGoBack())
{
webView.GoBack();
}
}
Вот мой MainActivity.cs
код
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
TextView txtURL;
Button btnGo;
Button btnForward;
Button btnBack;
WebView webView;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
//Get txtURL TextView, btnGo Button,webView WebView from Main Resource Layout.
txtURL = FindViewById<TextView>(Resource.Id.txtURL);
btnGo = FindViewById<Button>(Resource.Id.btnGo);
btnForward = FindViewById<Button>(Resource.Id.btnForward);
btnBack = FindViewById<Button>(Resource.Id.btnBack);
webView = FindViewById<WebView>(Resource.Id.webView1);
webView.Settings.JavaScriptEnabled = true;
//SetWebViewClient with an instance of WebViewClientClass
webView.SetWebViewClient(new WebViewClientClass());
webView.LoadUrl("file:///android_asset/Content/app.html");
//Enabled Javascript in Websettings
WebSettings websettings = webView.Settings;
websettings.JavaScriptEnabled = true;
//btnGo Click event
btnGo.Click += BtnGo_Click;
btnBack.Click += BtnBack_Click;
btnForward.Click += BtnForward_Click;
}
private void BtnBack_Click(object sender, EventArgs e)
{
//If WebView has back History item then navigate to the last visited page.
if (webView.CanGoBack())
{
webView.GoBack();
}
}
private void BtnForward_Click(object sender, EventArgs e)
{
//If WebView has forward History item then forward to the next visited page.
if (webView.CanGoForward())
{
webView.GoForward();
}
}
private void BtnGo_Click(object sender, System.EventArgs e)
{
var mytext=txtURL.Text;
Toast.MakeText(this, mytext+"", ToastLength.Short).Show();
//Load URL in txtURL in WebView.
webView.LoadUrl(txtURL.Text);
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
internal class WebViewClientClass : WebViewClient
{
//Give the host application a chance to take over the control when a new URL is about to be loaded in the current WebView.
public override bool ShouldOverrideUrlLoading(WebView view, string url)
{
view.LoadUrl(url);
return true;
}
}
Вот activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_height="wrap_content"
android:id="@+id/txtURL"
android:layout_width="match_parent"
android:hint="https://www.google.com" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayout1">
<Button
android:text="Back"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/btnBack"
android:layout_weight="1" />
<Button
android:text="Go"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/btnGo"
android:layout_weight="2" />
<Button
android:text="Forward"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/btnForward"
android:layout_weight="1" />
</LinearLayout>
<android.webkit.WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/webView1"/>
</LinearLayout >
Пожалуйста, не забудьте: добавьте разрешение INTERNET
в ваш AndroidManifest.xml
.
<uses-permission android:name="android.permission.INTERNET" />