Create Custom class for TextInputLayout

孤街浪徒 提交于 2021-02-18 11:31:12

问题


In TextInputLayout I want to add TextInputEditText via Custom class Here is the code for custom class I have created :

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);
    }
}

Here is the Xml code :

<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"/>

It doesn't show any preview of the custom view


回答1:


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);
        addView(editText);
    }
}



回答2:


Instead of extending it with TextInputLayout try extending it with constraint layout and add your custom view with editText and InputField then inflate view

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" />



来源:https://stackoverflow.com/questions/59249129/create-custom-class-for-textinputlayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!