【问题标题】:How can I run validation when my users exit textField in compose?当我的用户退出 textField 时如何运行验证?
【发布时间】:2023-01-21 05:49:28
【问题描述】:

我有一个函数可以检查我的 textField 是否有任何问题

乐趣 hasError(textFieldLabel:字符串):布尔值{}

我希望此功能仅在用户完成输入文本后运行。 当用户存在文本字段时,我们假设他们已经完成并且我在该字段上运行 hasError()

这是对我不起作用的

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

FocusDirection.Next 仅在您决定合适时移动焦点,但在用户决定从一个字段移动到另一个字段时不监听

Modifier.onFocusChanged{} 这不起作用,因为它在页面加载时触发,并调用 hasError(),使我所有的文本字段都处于错误状态

我想在用户退出文本字段时触发 hasError

【问题讨论】:

    标签: android kotlin android-jetpack-compose android-compose-textfield android-textattributes


    【解决方案1】:
    YourEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
      if (!hasFocus) { {
          //User leaved your edit text
      }
    }
    });
    

    【讨论】:

      【解决方案2】:

      您可以简单地监听编辑文本更改值。 onValueChange 在文本更改和文本完成时调用,

      【讨论】:

        猜你喜欢
        • 2020-03-10
        • 1970-01-01
        • 2021-08-05
        • 1970-01-01
        • 2019-06-08
        • 1970-01-01
        • 2019-06-05
        • 1970-01-01
        • 2012-09-03
        相关资源
        最近更新 更多