Changing keyboard's ImeOptions of Xamarin.Forms.Entry in custom renderer not working on Android

做~自己de王妃 提交于 2019-12-08 00:49:50

问题


What I have:

I have a custom class MyEntry derived from Xamarin.Forms.Entry and custom renderer classes MyEntryRenderer for Android and iOS.

What I want:

I want to change the keyboard's "enter"-button to a "search"-button by changing ImeOptions on Android and ReturnKeyType on iOS (see sample code). When I press the altered "search"-button, the MyEntry.Completed event should be called (like before when I pressed the un-altered "enter"-button.

What really happens:

On iOS the code works like expected. But on Android nothing happens. The event doesn't get called.

My question:

How can I achieve what I described above on Android?

Sample code:

App.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,
            };
        }
    }
}

MyEntry.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;
            }
        }

    }
}

回答1:


I found a workaround for my problem myself:

First I added an Action to my custom entry to be called when I press my "search"-button.

MyEntry.cs

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

Second I "listen" for ImeAction.Search like this and call the Action I added to my custom 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 = ImeAction.Search;
                Control.EditorAction += (sender, args) => {
                    if (args.ActionId == ImeAction.Search) {
                        var entry = (AddressEntry)Element;
                        entry.SearchPressed();
                    }
                };
            }
        }

    }
}

In a third class where I use MyEntry I can run some code when the "search"-button is pressed like this:

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



回答2:


public class EntryExtensionRenderer : EntryRenderer { protected override void OnElementChanged(ElementChangedEventArgs e) { base.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;
    }


}


来源:https://stackoverflow.com/questions/27690724/changing-keyboards-imeoptions-of-xamarin-forms-entry-in-custom-renderer-not-wor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!