在数据绑定中使用键盘上的完成按钮

Lea*_*ira 3 android android-databinding

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

例:

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

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

谢谢!

Geo*_*unt 11

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

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

public class OnOkInSoftKeyboardListener {
    void onOkInSoftKeyboard();
}
Run Code Online (Sandbox Code Playgroud)

然后你需要一个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();
                }
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)


Tos*_*osa 8

就像我自己在看这个一样,这里有一个更简单的版本,其中直接从数据绑定调用函数:

在您的 ViewModel 中使用此功能:

 public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
return false; // if you want the default action of the actionNext or so on
return true; // if you want to intercept
}
Run Code Online (Sandbox Code Playgroud)

在布局中:

android:onEditorAction="@{(view,actionId,event) -> viewModel.onEditorAction(view,actionId,event)}"
Run Code Online (Sandbox Code Playgroud)

  • 未知属性 onEditorAction @Tosa (2认同)

ers*_*tan 7

kapt使用Kotlin产生:

e: [kapt] An exception occurred: android.databinding.tool.util.LoggedErrorException: Found data binding errors.
****/ data binding error ****msg:Listener class kotlin.jvm.functions.Function1 with method invoke did not match signature of any method viewModel::signIn
Run Code Online (Sandbox Code Playgroud)

(因为viewModel::signIn是类型的KFunction1),因此我们不能使用方法引用。但是,如果我们viewModel在类型相关的显式变量内创建变量,则可以将该变量作为绑定的参数传递。(或只使用一个类)

Bindings.kt:

@BindingAdapter("onEditorEnterAction")
fun EditText.onEditorEnterAction(f: Function1<String, Unit>?) {

    if (f == null) setOnEditorActionListener(null)
    else setOnEditorActionListener { v, actionId, event ->

        val imeAction = when (actionId) {
            EditorInfo.IME_ACTION_DONE,
            EditorInfo.IME_ACTION_SEND,
            EditorInfo.IME_ACTION_GO -> true
            else -> false
        }

        val keydownEvent = event?.keyCode == KeyEvent.KEYCODE_ENTER 
            && event.action == KeyEvent.ACTION_DOWN

        if (imeAction or keydownEvent)
            true.also { f(v.editableText.toString()) }
        else false
    }
}
Run Code Online (Sandbox Code Playgroud)

MyViewModel.kt:

fun signIn(password: String) {
    Toast.makeText(context, password, Toast.LENGTH_SHORT).show()
}

val signIn: Function1<String, Unit> = this::signIn
Run Code Online (Sandbox Code Playgroud)

layout.xml:

<EditText
    android:id="@+id/password"
    app:onEditorEnterAction="@{viewModel.signIn}"
    android:imeOptions="actionDone|actionSend|actionGo"
    android:singleLine="true"/>
Run Code Online (Sandbox Code Playgroud)