Home > Back-end >  How can I run validation when my users exit textField in compose?
How can I run validation when my users exit textField in compose?

Time:01-21

I have a function that will check if my textField has any issues

fun hasError(textFieldLabel: String):Boolean{}

I want this function to run only when the user has finished entering text. When the user exists the text field, we assume they're finished and I run hasError() on that field

Here is what didn't work for me

val focusManager = LocalFocusManager.current focusManager.moveFocus(FocusDirection.Next)

FocusDirection.Next Only moves the focus when you decide its appropriate, but doesn't listen when the user decides to move from one field to another

Modifier.onFocusChanged{} This doesn't work because it triggers when the page loads, and calls hasError(), leaving all my textFields in an error state

I want to trigger hasError when the user exits a text field

CodePudding user response:

YourEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
  if (!hasFocus) { {
      //User leaved your edit text
  }
}
});

CodePudding user response:

You can simple listene for edit text change value. onValueChange is called when the text is changed and the text is finished,

  • Related