2017-10-12 95 views
0
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。您的自定義視圖也可以擴展直接查看此代碼不顯示該字段上的光標。我想了解如何正確擴展TextInputLayout類如何正確擴展TextInputLayout.java

+0

您沒有將phoneNumberEditText添加到PhoneInputLayout中以查看代碼中的子視圖。 – kimkevin

回答

0

我不知道爲什麼要爲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>