简体   繁体   中英

Xamarin Android with Mvvmcross. Binding an OnEditorAction in axml

The situation : I have a list of view which contains an EditText. I want the user to be able to modify the text and only send his new text to the viewmodel when he press on the done button on the keyboard. My edit text is binded has follow

<EditText
   android:id="@+id/textNumero"
   android:layout_width="0dp"
   android:layout_height="fill_parent"
   android:layout_weight="0.23"
   android:textColor="#ffffffff"
   local:MvxBind="Text BarilId" />

I found on the internet that i can you use this event to do what i want:

idEditText.OnEditorAction (ImeAction.Done) += //insert delegate here

Unfortunately, i can't reach my activity since i'm binding the class in my list.

So, i thought of binding a command in my class like this:

<EditText
       android:id="@+id/textNumero"
       android:layout_width="0dp"
       android:layout_height="fill_parent"
       android:layout_weight="0.23"
       android:textColor="#ffffffff"
       local:MvxBind="OnEditorAction EditCommand"
       local:MvxBind="Text BarilId" />

The command :

    private IMvxCommand _editCommand;
    public IMvxCommand EditCommand
    {
        get {
            _editCommand = _editCommand ?? new MvxCommand(() => {
                //do validation here
            });
            return _editCommand;
        }
    }

But I don't know how to pass the ImeAction.Done to my command or if i even receiving something like that.

Can i have some help on the remaining part?

You could create your custom EditText that has a command and then you could bind it in the axml.

The other option would be using:

idEditText.OnEditorAction (ImeAction.Done) += (ViewModel as MyViewModel).EditCommand.Execute(whateveryouwanttouse);

To reach the Activity you could use:

Mvx.Resolve<IMvxAndroidCurrentTopActivity>().Activity

Finally, i use Stuart answer in this : https://stackoverflow.com/a/19221385/1188639

I just changed the code a little so i can use my OnEditorAction in the SubscibeToEvents() and change the handle function too. The whole class look like this :

public class MvxEditTextEditForBinding: MvxAndroidTargetBinding
{
    protected EditText EditText
    {
        get { return (EditText)Target; }
    }

    private bool _subscribed;

    public MvxEditTextEditForBinding(EditText view)
        : base(view)
    {
    }

    protected override void SetValueImpl(object target, object value)
    {
        var editText = EditText;
        if (editText == null)
            return;

        value = value ?? string.Empty;
        editText.Text = value.ToString();
    }

    public override MvxBindingMode DefaultMode
    {
        get { return MvxBindingMode.TwoWay; }
    }

    public override void SubscribeToEvents()
    {
        var editText = EditText;
        if (editText == null)
            return;

        editText.EditorAction += HandleEditForChange;
        _subscribed = true;
    }

    private void HandleEditForChange(object sender, TextView.EditorActionEventArgs  e)
    {
        var editText = EditText;

        InputMethodManager imm = (InputMethodManager)editText.Context.GetSystemService(Context.InputMethodService); 
        imm.HideSoftInputFromWindow(editText.WindowToken, 0);


        e.Handled = false;
        if (e.ActionId == ImeAction.Done)
        {
            FireValueChanged(editText.Text);
            e.Handled = true;   
        }
    }

    public override Type TargetType
    {
        get { return typeof(string); }
    }

    protected override void Dispose(bool isDisposing)
    {
        if (isDisposing)
        {
            var editText = EditText;
            if (editText != null && _subscribed)
            {
                editText.EditorAction -= HandleEditForChange;
                _subscribed = false;
            }
        }
        base.Dispose(isDisposing);
    }
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM