【问题标题】:There is no textWatcher or setError method on third party editText library第三方 editText 库上没有 textWatcher 或 setError 方法
【发布时间】:2021-08-16 11:55:41
【问题描述】:

我正在开发一个使用浮动提示编辑文本库的应用程序。那个库是

实现 'com.github.thomhurst:ExpandableHintText:1.0.7' 链接:-https://github.com/thomhurst/ExpandableHintText

但是其中没有访问一些函数,例如 setError()、addOnTextChangeListner() 等。 现在我能做什么我无法得到任何解决方案。我无法更改库,因为我的应用程序 ui 依赖于此。

感谢您的回答。

【问题讨论】:

  • 你还没有真正问过问题,如果那个库不允许你这样做,那么你就不能这样做
  • 我现在能做什么
  • 您可以使用android提供的标准材料textinput布局,您可以克隆该库并自己构建您需要的所有功能,或者您可以向库的作者寻求帮助
  • 是否有任何替代 textWatcher 用于实时编辑文本更改数据
  • @Hiten 可以使用底层的EditText 并在其上设置错误、文本观察器等。请参阅下面的答案。

标签: java android android-edittext


【解决方案1】:

虽然ExpandableHintText 小部件本身没有设置错误或文本观察器的方法,但useEditText 方法可以使用底层EditText

这是一个使用它来设置错误的示例:

yourHintTextWidget.useEditText { editText -> editText.error = "Very wrong, so error, wow" }

请注意,我没有测试过这个,我只是在源代码中找到它。

【讨论】:

    【解决方案2】:

    它不起作用,因为该库的开发人员已经创建了从 Frame Layout 扩展的 ExpandableTextHint,您可以查看此

    https://github.com/thomhurst/ExpandableHintText/blob/master/expandablehinttext/src/main/java/com/tomlonghurst/expandablehinttext/ExpandableHintText.kt.

    所以你可以做的是像这样使用Material Text Input Layout,

    <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/textInputFirstName"
            android:layout_height="wrap_content"
            android:textColorHint="@color/textInputLabelTextColor"
            app:errorEnabled="true"
            app:errorIconDrawable="@drawable/ic_error"
            app:hintTextColor="@color/textInputLabelTextColor"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/userProfilePhoto"
            app:shapeAppearance="?attr/shapeAppearanceSmallComponent">
    
            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/inputName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@null"
                android:maxLength="25"
                android:imeOptions="actionDone"
                android:textColor="@color/textInputTextColor" />
    
        </com.google.android.material.textfield.TextInputLayout>
    

    我知道与那个库相比,这是很多代码,但绝对值得。

    您还可以进一步自定义此官方文档。

    https://material.io/components/text-fields/android

    【讨论】:

      猜你喜欢
      • 2012-11-01
      • 2012-02-25
      • 2012-08-20
      • 2015-10-02
      • 1970-01-01
      • 2019-08-16
      • 1970-01-01
      • 2018-01-24
      • 2021-11-16
      相关资源
      最近更新 更多