NiAu
NiAu

Reputation: 627

Xamarin Forms Android key event handling on pages

Currently I'm developing a PCL Xamarin Forms for Android. I'm now working with an Entry which catch scanned data via Text_Changed event. I want to know if it is possible to handle an event on the contentpage for this scanned data. I'm missing something like KeyPress in Xamarin. Does anyone has any solutions?

Upvotes: 0

Views: 1728

Answers (1)

Bruno Caceiro
Bruno Caceiro

Reputation: 7199

You can use a CustomEntry.

In Forms add a new class:

public class CustomEntry : Entry
{
    public Action DonePressed = delegate {};

}

In your Android Project, add the CustomEntryRenderer:

 class CustomEntryRenderer : EntryRenderer
 {
    private CustomEntry customEntry;

    protected override void OnElementChanged (ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged (e);
        if (Control != null) 
        {                
            Control.ImeOptions = ImeAction.Done;
            Control.EditorAction += (sender, args) => {
                if (args.ActionId == ImeAction.Done) {                  
                    var entry = (CustomEntry)Element;
                    entry.DonePressed();
                }
            };
        }       

Upvotes: 1

Related Questions