更改键盘的 Ime选项 Xamarin.Forms.自定义渲染器中的条目在 Android 上不起作用



我有什么:

我有一个自定义类MyEntry派生自Xamarin.Forms.Entry和自定义渲染器类MyEntryRenderer适用于 Android 和 iOS。

我想要什么:

我想通过更改Android上的ImeOptionsiOS上的ReturnKeyType来将键盘的"输入"按钮更改为"搜索"按钮(请参阅示例代码)。当我按下更改的"搜索"按钮时,应该调用MyEntry.Completed事件(就像以前我按下未更改的"输入"按钮一样。

实际发生的情况:

iOS上,代码按预期工作。但Android什么也没发生。事件不会被调用。

我的问题:

我怎样才能实现我上面描述的Android

示例代码:

应用.cs:

namespace CustomEntry
{
    public class App
    {
        public static Page GetMainPage()
        {    
            MyEntry entry = new MyEntry {
                VerticalOptions = LayoutOptions.CenterAndExpand,
                HorizontalOptions = LayoutOptions.CenterAndExpand,
                Placeholder = "Enter some text"
            };
            entry.Completed += delegate {
                Console.WriteLine("Completed");
            };
            return new ContentPage { 
                Content = entry,
            };
        }
    }
}

我的条目.cs:

namespace CustomEntry
{
    public class MyEntry:Entry
    {
    }
}

MyEntryRenderer.cs (Android):

[assembly: ExportRenderer(typeof(MyEntry), typeof(MyEntryRenderer))]
namespace CustomEntry.Android
{
    public class MyEntryRenderer:EntryRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged(e);
            if (Control != null) { 
                Control.ImeOptions = global::Android.Views.InputMethods.ImeAction.Search;
            }
        }
    }
}

MyEntryRenderer.cs (iOS):

[assembly: ExportRenderer(typeof(MyEntry), typeof(MyEntryRenderer))]
namespace CustomEntry.iOS
{
    public class MyEntryRenderer:EntryRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged(e);
            if (Control != null) {
                Control.ReturnKeyType = UIReturnKeyType.Search;
            }
        }
    }
}

我自己找到了解决问题的方法:

首先,我在自定义条目中添加了一个Action,以便在按下"搜索"按钮时调用。

我的条目.cs

namespace CustomEntry
{
    public class MyEntry:Entry
    {
        public Action SearchPressed = delegate {
        };
    }
}

其次,我"监听"这样的ImeAction.Search,并调用我添加到自定义条目中的Action

MyEntryRenderer.cs (Android):

[assembly: ExportRenderer(typeof(MyEntry), typeof(MyEntryRenderer))]
namespace CustomEntry.Android
{
    public class MyEntryRenderer:EntryRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged(e);
            if (Control != null) {
                Control.ImeOptions = ImeAction.Search;
                Control.EditorAction += (sender, args) => {
                    if (args.ActionId == ImeAction.Search) {
                        var entry = (AddressEntry)Element;
                        entry.SearchPressed();
                    }
                };
            }
        }
    }
}

在我使用MyEntry的第三类中,当按下"搜索"按钮时,我可以运行一些代码:

var myEntry = new MyEntry();
myEntry.SearchPressed += SomeMethod;

public class EntryExtensionRenderer : EntryRenderer { protected override void OnElementChanged(ElementChangedEventArgs e) { 基础。OnElementChanged(e);

        if ((Element as EntryExtension).NoSuggestionsKey)
        {
            Control.AutocorrectionType = UITextAutocorrectionType.No;
        }
        if ((Element as EntryExtension).ReturnKeyType.Equals("Done"))
        {
            this.AddDoneButton("Done", (EntryExtension)Element);
        }
        else if ((Element as EntryExtension).ReturnKeyType.Equals("Next"))
        {
            this.AddDoneButton("Next", (EntryExtension)Element);
        }
    }
    protected void AddDoneButton(string button, EntryExtension entry)
    {
        UIToolbar toolbar = new UIToolbar(new RectangleF(0.0f, 0.0f, 50.0f, 44.0f));
        var doneButton = new UIBarButtonItem();
        if (button.Equals("Done")) { 
            doneButton = new UIBarButtonItem(UIBarButtonSystemItem.Done, delegate
            {
                entry.KeyPressedEnter();
            });
        }
        if (button.Equals("Next"))
        {
            doneButton = new UIBarButtonItem("Next", UIBarButtonItemStyle.Done, delegate
            {
                entry.KeyPressedEnter();
            });
        }
        toolbar.Items = new UIBarButtonItem[] {
            new UIBarButtonItem (UIBarButtonSystemItem.FlexibleSpace),
            doneButton
        };
        this.Control.InputAccessoryView = toolbar;
    }

}

相关内容

  • 没有找到相关文章

最新更新