在数据绑定中使用键盘上的完成按钮 [英] Use Done button on Keyboard in Databinding

查看:34
本文介绍了在数据绑定中使用键盘上的完成按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用软键盘的完成按钮通过数据绑定激活方法.就像 onClick 一样.有没有办法做到这一点?

I am trying to use the done button of the soft keyboard to activate a method via databinding. Just like onClick. Is there a way to do that?

示例:

<EditText               
    android:id="@+id/preSignUpPg2EnterPhone"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"       
    onOkInSoftKeyboard="@{(v) -> viewModel.someMethod()}"
    />

onOkInSoftKeyboard 不存在...有什么可以创建这种行为的吗?

onOkInSoftKeyboard doesn't exists... Is there something to create this behavior?

谢谢!

推荐答案

我不会声称自己是 onEditorAction() 或软键盘方面的专家.也就是说,假设您使用 Firoz Memon 建议的堆栈溢出问题的解决方案,您可以实现它.即使有其他更好的解决方案,这也可以让您了解如何添加自己的事件处理程序.

I won't claim to be an expert in onEditorAction() or soft keyboard. That said, assuming you use the solution to the stack overflow question Firoz Memon suggested, you can make it happen. Even if there is another solution that works better, this can give you an idea on how to add your own event handlers.

您需要一个带有某种处理程序的绑定适配器.假设您有一个像这样的空侦听器:

You'd need a binding adapter that takes some kind of handler. Let's assume you have an empty listener like this:

public class OnOkInSoftKeyboardListener {
    void onOkInSoftKeyboard();
}

那么你需要一个 BindingAdapter:

Then you need a BindingAdapter:

@BindingAdapter("onOkInSoftKeyboard") // I like it to match the listener method name
public static void setOnOkInSoftKeyboardListener(TextView view,
        final OnOkInSoftKeyboardListener listener) {
    if (listener == null) {
        view.setOnEditorActionListener(null);
    } else {
        view.setOnEditorActionListener(new OnEditorActionListener() {
            @Override
            public void onEditorAction(TextView v, int actionId, KeyEvent event) {
                // ... solution to receiving event
                if (somethingOrOther) {
                    listener.onOkInSoftKeyboard();
                }
            }
        });
    }
}

这篇关于在数据绑定中使用键盘上的完成按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆