【问题标题】:How to make a custom MultiAutoCompleteTextView inside a Material TextInputLayout?如何在 Material TextInputLayout 中制作自定义 MultiAutoCompleteTextView?
【发布时间】:2020-08-06 19:05:50
【问题描述】:

我想自定义一个MultiAutoCompleteTextView,为了使用Material,为了底层代码的可重用性和可更新性,它将被放置在TextInputLayout中。

我还没有开始,但我已经遇到了问题。

Graphic demostration of the problem

如您所见,第一个TextInputLayout 是完全错误的。 它们都具有完全相同的结构和属性,唯一不同的是自定义视图。 它基本上是一个扩展 MultiAutoCompleteTextView 的空类。

下面是xml和自定义视图类。

<com.google.android.material.textfield.TextInputLayout
        android:id="@+id/custom_view"
        style="@style/Theme.CustomViews.MorphInput"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@id/standard_view"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_chainStyle="packed">

        <com.scitalys.customViews.ui.main.MorphInputText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/hint" />

    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/standard_view"
        style="@style/Theme.CustomViews.MorphInput"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/custom_view">

        <MultiAutoCompleteTextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/hint" />

    </com.google.android.material.textfield.TextInputLayout>
class MorphInputText @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : androidx.appcompat.widget.AppCompatMultiAutoCompleteTextView(context, attrs, defStyleAttr) {

    init {
        this.threshold = 1
        val textWatcher = MorphInputTextWatcher
        this.addTextChangedListener(textWatcher)
    }

}

【问题讨论】:

    标签: android material-ui android-custom-view android-textinputlayout multiautocompletetextview


    【解决方案1】:

    在构造函数中更改defStyleAttr

    class MorphInputText @JvmOverloads constructor(
            context: Context,
            attrs: AttributeSet? = null,
            defStyleAttr: Int = R.attr.autoCompleteTextViewStyle
    ) : androidx.appcompat.widget.AppCompatMultiAutoCompleteTextView(context, attrs, defStyleAttr){
    
    }
    

    【讨论】:

      猜你喜欢
      • 2019-07-17
      • 2019-02-09
      • 2016-07-16
      • 2020-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-29
      • 1970-01-01
      相关资源
      最近更新 更多