Xamarin Andorid Webview文本选择无法使用自定义上下文菜单

时间:2017-07-18 15:21:06

标签: android xamarin webview contextmenu

当重写OnCreateContextMenu时,Xamarin Andorid Webview不允许选择文本,以便在上下文菜单中有一个新的菜单项。

此外,它不显示默认菜单。因此,我们希望在添加新菜单时保留现有的默认菜单,如复制,全选等。

protected override void OnCreateContextMenu(Android.Views.IContextMenu menu)
{
     base.OnCreateContextMenu(menu);
     var item = menu.Add("Add To Notes");
     item.SetOnMenuItemClickListener(this);
}

1 个答案:

答案 0 :(得分:0)

  

我们希望在添加新菜单时保留现有的默认菜单,如复制,全选等。

您可以覆盖Menu方法,在默认Activity上添加新菜单。覆盖此方法,您可以通知WebView view = FindViewById<WebView>(Resource.Id.web); view.Settings.JavaScriptEnabled = true; view.SetWebViewClient(new MyWebViewClient()); view.LoadUrl("https://www.google.com.sg/?gfe_rd=cr&ei=gFDGWIjIH8LB9AXbwY3QDg"); public class MyWebViewClient : WebViewClient { public override bool ShouldOverrideUrlLoading(WebView view, string url) { view.LoadUrl(url); return true; } } 已启动操作模式。这是我的代码,它工作正常:

public override void OnActionModeStarted(ActionMode mode)
{
    IMenu menu = mode.Menu;
    menu.Add("Add To Notes");
    menu.GetItem(0).SetOnMenuItemClickListener(new MyMenuItemOnMenuItemClickListener(this));
    base.OnActionModeStarted(mode);
}

保留现有的默认菜单,同时添加新菜单:

ItemClickListerer

在新menu上设置menu.GetItem(0).SetOnMenuItemClickListener(new MyMenuItemOnMenuItemClickListener(this)); public class MyMenuItemOnMenuItemClickListener : Java.Lang.Object, IMenuItemOnMenuItemClickListener { private MainActivity mContext; public MyMenuItemOnMenuItemClickListener(MainActivity activity) { this.mContext = activity; } public bool OnMenuItemClick(IMenuItem item) { Toast.MakeText(mContext, "Hi", ToastLength.Short).Show(); return true; } }

WebView

效果如this

编辑

使用此自定义WebView行为可能非常简单,您只需添加一个新菜单,我在上面指出并初始化Renderer中的public class WebPage : ContentPage { public WebPage() { var browser = new MyVebView(); browser.Source = "https://www.google.com.sg/?gfe_rd=cr&ei=gFDGWIjIH8LB9AXbwY3QDg"; Content = browser; } } ,这是我的代码:< / p>

在Xamarin.Forms中:

WebViewRenderer

[assembly: ExportRenderer(typeof(MyVebView), typeof(MyVebViewRenderer))] namespace FormsWebView.Droid { public class MyVebViewRenderer : WebViewRenderer { Android.Webkit.WebView webView; protected override void OnElementChanged(ElementChangedEventArgs<WebView> e) { base.OnElementChanged(e); if(Control == null) { webView = new Android.Webkit.WebView(Context); webView.Settings.JavaScriptEnabled = true; webView.SetWebViewClient(new MyWebViewClient()); webView.LoadUrl("https://www.google.com.sg/?gfe_rd=cr&ei=gFDGWIjIH8LB9AXbwY3QDg"); SetNativeControl(webView); } } } }

onActionModeStarted

覆盖namespace FormsWebView.Droid { [Activity(Label = "FormsWebView", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)] public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity { protected override void OnCreate(Bundle bundle) { TabLayoutResource = Resource.Layout.Tabbar; ToolbarResource = Resource.Layout.Toolbar; base.OnCreate(bundle); global::Xamarin.Forms.Forms.Init(this, bundle); LoadApplication(new App()); } public override void OnActionModeStarted(ActionMode mode) { //... } } public class MyWebViewClient : WebViewClient { //... } public class MyMenuItemOnMenuItemClickListener : Java.Lang.Object, IMenuItemOnMenuItemClickListener { //... } } 方法以添加新菜单,如上所述:

{{1}}