Xamarin Android EditText enter key
Asked Answered
L

6

16

I started to work with Xamarin Studio a weeks ago, and could not find solution to the next problem: created an edittext which will contains serial numbers. I'd like ro run a function after the Enter was pressed. It's working fine, when I press Enter, the function runs without failure, but I can not modify the content of the edittext (I can't type into it).

The code:

EditText edittext_vonalkod = FindViewById<EditText>(Resource.Id.editText_vonalkod);
edittext_vonalkod.KeyPress += (object sender, View.KeyEventArgs e) =>
{
    if ((e.Event.Action == KeyEventActions.Down) && (e.KeyCode == Keycode.Enter))
    {
        //Here is the function
    }
};

This is the code of the control:

<EditText
    p1:layout_width="wrap_content"
    p1:layout_height="wrap_content"
    p1:layout_below="@+id/editText_dolgozo_neve"
    p1:id="@+id/editText_vonalkod"
    p1:layout_alignLeft="@+id/editText_dolgozo_neve"
    p1:hint="Vonalkód"
    p1:text="1032080293"
    p1:layout_toLeftOf="@+id/editText_allapot" />

I tried to use edittext_vonalkod.TextCanged with its arguments, the problem reserved. I can modify the content but can not handle Enter key.

Thanks!

Lubow answered 18/4, 2013 at 18:5 Comment(0)
P
19

The best approach would be to use the EditorAction event that is designed to be triggered on the Enter key press. It would be a code like this:

edittext_vonalkod.EditorAction += (sender, e) => {
    if (e.ActionId == ImeAction.Done) 
    {
        btnLogin.PerformClick();
    }
    else
    {
        e.Handled = false;
    }
};

And to be able to change the text of the Enter button use imeOptions on your XML:

<EditText
    p1:layout_width="wrap_content"
    p1:layout_height="wrap_content"
    p1:layout_below="@+id/editText_dolgozo_neve"
    p1:id="@+id/editText_vonalkod"
    p1:layout_alignLeft="@+id/editText_dolgozo_neve"
    p1:hint="Vonalkód"
    p1:text="1032080293"
    p1:layout_toLeftOf="@+id/editText_allapot" 
    p1:imeOptions="actionSend" />
Pinery answered 22/11, 2014 at 16:36 Comment(1)
How to hide the keyboard after?Romo
N
5

You need to mark the event as not being handled when the pressed key is ENTER. Place the following code inside your KeyPress handler.

if (e.Event.Action == KeyEventActions.Down && e.KeyCode == Keycode.Enter) 
{
   // Code executed when the enter key is pressed down
} 
else 
{
   e.Handled = false;
}
Norbertonorbie answered 20/5, 2013 at 17:45 Comment(0)
B
3

try this:



    editText = FindViewById(Resource.Id.editText);    
    editText.KeyPress += (object sender, View.KeyEventArgs e) => 
    {
        e.Handled = false;
        if (e.Event.Action == KeyEventActions.Down && e.KeyCode == Keycode.Enter)
        {
            //your logic here
            e.Handled = true;
        }
    };

Battery answered 27/10, 2015 at 15:22 Comment(0)
H
1

Even better is to create reusable extension to EditText (EditTextExtensions.cs):

public static class EditTextExtensions
{
    public static void SetKeyboardDoneActionToButton(this EditText editText, Button button)
    {
        editText.EditorAction += (sender, e) => {
            if (e.ActionId == ImeAction.Done)
            {
                button.PerformClick();
            }
            else
            {
                e.Handled = false;
            }
        };
    }
}
Hydrated answered 8/9, 2016 at 9:26 Comment(0)
A
0

The Android EditText component must be focused. You can force it with:

editText.RequestFocus();
Arlenaarlene answered 1/6, 2022 at 9:16 Comment(0)
C
-1
 editText.KeyPress += (object sender, View.KeyEventArgs e) =>
            {
                    if ((e.KeyCode == Keycode.Enter))
                    {
                       // `enter code here`
                    }
            };
Cruz answered 24/5, 2018 at 6:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.