为TextInputLayout创建自定义类

时间:2019-12-09 12:40:29

标签: android android-textinputlayout

在TextInputLayout中,我想通过Custom类添加TextInputEditText 这是我创建的自定义类的代码:

public class CustomTextInputEditText extends TextInputLayout {

private TextInputEditText editText;

public CustomTextInputEditText(@NonNull Context context) {
    this(context, null);
}

public CustomTextInputEditText(@NonNull Context context, @Nullable AttributeSet attrs) {
    this(context, attrs, R.style.Widget_MaterialComponents_TextInputLayout_OutlinedBox);
}

public CustomTextInputEditText(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(context, attrs, defStyleAttr);
}

private void init(Context context, AttributeSet attrs, int defStyleAttr) {
    removeAllViews();
    setWillNotDraw(false);
    editText = new TextInputEditText(getContext());
    createEditBox(editText);
}

private void createEditBox(TextInputEditText editText) {
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    editText.setPadding(0,10,0,0);
    editText.setLayoutParams(layoutParams);
    }
}

这是Xml代码:

<com.example.myapplication.CustomTextInputEditText
    android:id="@+id/custom_view"
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:boxStrokeColor="@color/colorAccent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="parent"/>

它不显示自定义视图的任何预览

2 个答案:

答案 0 :(得分:1)

__asm__(".symver old_foo,foo@VERS_1.1");

答案 1 :(得分:0)

与其使用TextInputLayout进行扩展,不如尝试使用约束布局来对其进行扩展,并使用editTextInputField添加自定义视图,然后对视图进行充气

  

custom_material_edit_text.xml

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/textInputLayout"
    style="@style/contentInputLayoutStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="@dimen/_8dp"
    android:paddingStart="5dp"
    android:paddingLeft="5dp"
    android:layout_marginLeft="@dimen/_8dp"
    android:layout_marginEnd="@dimen/_8dp"
    android:layout_marginRight="@dimen/_8dp"
    app:errorEnabled="false"
    app:layout_constraintBottom_toTopOf="@+id/errorTextView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:hint="@string/EnterMobileNumber">


    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/textInputEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="phone"
        android:gravity="start"
        android:textDirection="anyRtl"
        tools:text="03-xxxxxxx"
        android:background="@null"
        android:paddingEnd="@dimen/inputFieldPadding_36dp"
        android:paddingRight="@dimen/inputFieldPadding_36dp" />

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



<ImageView
    android:id="@+id/cancelButton"
    android:layout_width="@dimen/inputIcon_20dp"
    android:layout_height="@dimen/inputIcon_20dp"
    android:layout_marginEnd="@dimen/iconMargin_16dp"
    android:layout_marginRight="@dimen/iconMargin_16dp"
    android:src="@mipmap/close"
    android:visibility="gone"
    app:layout_constraintBottom_toBottomOf="@+id/textInputLayout"
    app:layout_constraintEnd_toEndOf="@+id/textInputLayout"
    app:layout_constraintTop_toTopOf="@+id/textInputLayout"
    app:layout_constraintVertical_bias=".48" />


<ImageView
    android:id="@+id/keyboardButton"
    android:layout_width="@dimen/inputIcon_20dp"
    android:layout_height="@dimen/inputIcon_20dp"
    android:layout_marginEnd="@dimen/iconMargin_16dp"
    android:layout_marginRight="@dimen/iconMargin_16dp"
    android:src="@mipmap/label_keyboard"
    android:visibility="visible"
    app:layout_constraintBottom_toBottomOf="@+id/textInputLayout"
    app:layout_constraintEnd_toEndOf="@+id/textInputLayout"
    app:layout_constraintRight_toRightOf="@+id/textInputLayout"
    app:layout_constraintTop_toTopOf="@+id/textInputLayout"
    app:layout_constraintVertical_bias=".48" />


<TextView
    android:id="@+id/errorTextView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="@dimen/_8dp"
    android:layout_marginLeft="@dimen/_8dp"
    android:layout_marginEnd="@dimen/_8dp"
    android:layout_marginRight="@dimen/_8dp"
    android:visibility="gone"
    android:textColor="@color/red"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textInputLayout"
    tools:text="Invalid data" />

  inflate(context, R.layout.custom_material_edit_text, this);

   <com.example.widget.MaterialInputView
        android:id="@+id/materialEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginEnd="10dp"
         app:floatingLabel="Amount (Rs.)"
        android:inputType="number"
        android:hint="00000-XXXXXX" />

outPut