b.holz
b.holz

Reputation: 237

Xamarin.Android invoking the editor of EditText manually

I have an EditText (called Stock). I added a EditorAction to Stock. When I click on Stock the editor is opened and everything works as intended. Now I want the same behaviour but invoked from a different part in the code. I tried to call Stock.CallOnClick() but nothing happened. I was under the impression that the opening of the Editor was caused by the OnClick() event.

What would be the correct call to simulate the same behaviour of a tap of the EditText?

Thanks.

Upvotes: 0

Views: 340

Answers (2)

b.holz
b.holz

Reputation: 237

So you need to call the InputMethodManager and call ShowSoftInput() where you want it and HideSoftInput() in your editoraction event.

        buttonStock.Click += delegate
        {
            stock.RequestFocus(); // this seems to be necessary
            stock.SelectAll(); // this is convenient
            var imm = ((InputMethodManager)GetSystemService(InputMethodService));
            imm.ShowSoftInput(stock, ShowFlags.Forced);
        };

stock is my EditText.

Upvotes: 0

Elvis Xia - MSFT
Elvis Xia - MSFT

Reputation: 10831

Now I want the same behaviour but invoked from a different part in the code. I tried to call Stock.CallOnClick() but nothing happened.

If I understand invoked from a different part in the code correctly, you need to use EditText.SetOnEditorActionListener(TextView.IOnEditorActionListener):

  1. Create a new class inherits java.lang.object and TextView.IOnEditorActionListener and implement it:

    public class MyAction : Java.Lang.Object, TextView.IOnEditorActionListener
    {
    
        public void Dispose()
        {
            this.Dispose();
        }
    
        public bool OnEditorAction(TextView v, [GeneratedEnum] ImeAction actionId, KeyEvent e)
        {
            //insert your codes here
            return true;
        }
    
    }
    
  2. Then you can pass an instance of MyAction to SetOnEditorActionListener:

    etStock = FindViewById<EditText>(Resource.Id.etStock);
    etStock.SetOnEditorActionListener(new MyAction());
    

Upvotes: 1

Related Questions