Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass views in OnFocusChange in data binding Android

Tags:

android

I am trying to pass views using Listener binding in onFocusChange function but get the following error

The callback android.view.View.OnFocusChangeListener#onFocusChange has 2 methods but the lambda defined has 1. It should have either 0 or equal number of parameters. 

Please can somebody help me in finding the right way to implement this.

    <variable
        name="user"
        type="com.webage.www.vco_address.User" />

    <variable
        name="obj"
        type="com.webage.www.colin.Validations" />



</data>
    <EditText
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="82dp"
        android:text="@={user.name}"
        android:onFocusChange="@{(view)->obj.saveClick(view, user.name)}" />

Validations

public class Validations {
    public void saveClick(View view, String name){
        Log.v("saveClick",name);
    }
}
like image 779
Colin11 Avatar asked Jan 05 '23 10:01

Colin11


2 Answers

I believe that the error message is worded incorrectly. It doesn't make any sense that onFocusChange() has two methods since it is a method. More likely, the problem is that onFocusChange() has two parameters, a View and a boolean. You have declared the lambda to take only one parameter. Just add a second parameter to the lambda.

like image 61
Code-Apprentice Avatar answered Jan 14 '23 01:01

Code-Apprentice


for xml:

app:onFocusChangeListener = "@{viewModel::onTargetFocusChanged}"

for program code:

fun onTargetFocusChanged(view: View, hasFocus: Boolean){
        ...
}
like image 30
강준영 Avatar answered Jan 14 '23 01:01

강준영