如何正确扩展TextInputLayout.java

时间:2017-10-12 19:17:16

标签: java android android-custom-view

    public class PhoneInputLayout extends TextInputLayout {
  public void setPhoneNumberEditText(TextInputEditText 
         phoneNumberEditText) {
      this.phoneNumberEditText = phoneNumberEditText;
  }

  private TextInputEditText phoneNumberEditText;
  public PhoneInputLayout(Context context ) {
    super(context);
  }

  public PhoneInputLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
  }



  @Override public void setError(@Nullable CharSequence error) {
    phoneNumberEditText.setPaddingRelative(phoneNumberEditText.getPaddingStart() - getPadding(R.dimen.account_info_margin),
        phoneNumberEditText.getPaddingTop(),
        phoneNumberEditText.getPaddingEnd(),
        phoneNumberEditText.getPaddingBottom());
    super.setError(error);

  }

  private int getPadding(int paddingId) {
    float scale = this.getContext().getResources().getDisplayMetrics().density;
    return (int) (this.getContext().getResources().getDimension(paddingId) * scale + 0.5f);
  }
}

Android框架中定义的所有视图类都扩展了View。您的自定义视图也可以直接扩展View此代码不会在该字段上显示光标。我想了解如何正确扩展TextInputLayout类

1 个答案:

答案 0 :(得分:1)

我不知道您为什么要为TextInputLayout创建自定义视图。 以下内容适用于该字段上的光标。

<android.support.design.widget.TextInputLayout
android:id="@+id/text_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:hint="Phone Number"
android:labelFor="@id/card_input_password"
android:paddingStart="12dp"
android:textColorHint="@color/text_tertiary_black"
app:errorEnabled="true"
app:hintTextAppearance="@style/TextInputLayoutFlybuysTheme">

    <EditText
    android:id="@id/card_input_password"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="top|start"
    android:inputType="phone" />

</android.support.design.widget.TextInputLayout>