要从Xamarin Android的WebView中加载的登录页面的username文本框中读取值,你需要执行以下步骤:
以下是一个示例代码,展示如何在Xamarin Android中从WebView加载的登录页面的username文本框中读取值:
using Android.App;
using Android.OS;
using Android.Webkit;
using Android.Widget;
using System.Threading.Tasks;
namespace XamarinWebViewExample
{
[Activity(Label = "WebViewExample", MainLauncher = true)]
public class MainActivity : Activity
{
private WebView webView;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Main);
webView = FindViewById<WebView>(Resource.Id.webview);
webView.Settings.JavaScriptEnabled = true;
webView.LoadUrl("https://example.com/login");
webView.SetWebViewClient(new WebViewClient()
{
OnPageFinished = async (sender, e) =>
{
await ReadUsername();
}
});
}
private async Task ReadUsername()
{
var js = @"
function readUsername() {
var username = document.getElementById('username').value;
return username;
}
";
var username = await webView.EvaluateJavascriptAsync(js + "readUsername();");
Toast.MakeText(this, "Username: " + username, ToastLength.Long).Show();
}
}
}
webView.Settings.JavaScriptEnabled = true;
webView.LoadUrl("https://example.com/login");
ReadUsername
方法。EvaluateJavascriptAsync
方法执行JavaScript代码。通过这种方式,你可以从Xamarin Android WebView中加载的登录页面的username文本框中读取值。
领取专属 10元无门槛券
手把手带您无忧上云