首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MvxBind on actionSend (imeOption动作)

MvxBind on actionSend (imeOption动作)
EN

Stack Overflow用户
提问于 2013-12-30 11:04:31
回答 1查看 1.1K关注 0票数 1

我有一个这样的editText:

代码语言:javascript
复制
    <EditText
        local:MvxBind="Text MyProperty; Click MyCommand"
        android:inputType="textShortMessage"
        android:imeOptions="actionSend" />

我希望该命令由键盘上的"enter/action“键触发。我应该在绑定中使用什么动词?我目前使用的“更改”动词在控件中的任何触摸上都使用触发器!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-30 20:21:34

如果您想要实现“自定义绑定”,那么有三种方法可以实现:

  1. 实现并注册真正的自定义绑定
  2. 实现一个自定义控件,该控件公开要绑定的自定义属性。
  3. 使用公开自定义属性的中间对象

https://stackoverflow.com/a/19221385/373321中的答案显示了一个真正的自定义绑定。

从标准视图继承的自定义控件将在N=18中http://mvvmcross.blogspot.com中讨论-这里的一个例子可能是:

代码语言:javascript
复制
public class DoneEditText : EditText, TextView.IOnEditorActionListener
{
    public DoneEditText(Context context) : base(context)
    {
        Init();
    }

    public DoneEditText(Context context, IAttributeSet attrs) : base(context, attrs)
    {
        Init();
    }

    public DoneEditText(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle)
    {
        Init();
    }

    private void Init()
    {
          this.SetOnEditorActionListener(this);
    }

    public ICommand DoneAction { get;set; ]

    #region Implementation of IOnEditorActionListener

    public bool OnEditorAction(TextView v, ImeAction actionId, KeyEvent e)
    {
        if (actionId == ImeAction.Done)
        {
            if (DoneAction != null)
                    DoneAction.Execute(v.Text);
            return true;
        }

        return false;
    }

    #endregion
}    

使用中间对象进行绑定将使用如下类:

代码语言:javascript
复制
public class DoneActionListener : Java.Lang.Object, TextView.IOnEditorActionListener
{
    public ICommand DoneAction { get; set; }

    public DoneActionListener(TextView v)
    {
          v.SetOnEditorActionListener(this);
   }

    #region Implementation of IOnEditorActionListener

    public bool OnEditorAction(TextView v, ImeAction actionId, KeyEvent e)
    {
        if (actionId == ImeAction.Done)
        {
            if (DoneAction != null)
                    DoneAction.Execute(v.Text);
            return true;
        }

        return false;
    }

    #endregion
}

这可以在OnCreate中使用Fluent绑定来处理,如下所示:

代码语言:javascript
复制
private DoneActionListener _listener;

public override OnCreate(Bundle b)
{
    base.OnCreate(b);
    SetContentView(Resource.Layout.MyLayoutId);

    _listener = new DoneActionListener(FindViewById<EditText>(Resource.Id.MyEditId));

    var set = this.CreateBindingSet<MyView, MyViewModel>();
    set.Bind(_listener).For(v => v.DoneAction).To(vm => vm.TheDoneCommand);
    set.Apply();
}
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20838217

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档