我的登录活动中有自定义textinputlayout 同样在自定义textinputlayout布局中我有自定义可清除的edittext。
我无法在登录活动屏幕的预览中看到自定义textinputlayout,但是当我在设备上运行该应用时,会显示该内容。
登录活动的xml
<com.santsg.paximum.ui.customview.component.SpecialTextInputLayout
android:id="@+id/userNameEdt"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:sethint="@string/user_name"/>
<com.santsg.paximum.ui.customview.component.SpecialTextInputLayout
android:id="@+id/passwordEdt"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:sethint="@string/password"/>
CustomTextInputLayout
public class SpecialTextInputLayout extends LinearLayout {
ClearableEditText editText;
TextInputLayout layout;
String hint;
public SpecialTextInputLayout(Context context) {
this(context, null);
}
public SpecialTextInputLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public SpecialTextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs);
LayoutInflater.from(getContext()).inflate(R.layout.special_edittext_layout, this);
initResource();
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ClearableEditText, defStyleAttr, 0);
try {
hint = ta.getString(R.styleable.ClearableEditText_sethint);
} finally {
ta.recycle();
}
layout.setHint(hint);
}
private void initResource() {
editText = findViewById(R.id.specialEdt);
layout = findViewById(R.id.specialEditLayout);
}
public void setText(String text) {
editText.setText(text);
}
public String getText() {
return editText.getText().toString();
}
自定义文字输入布局&x; xml。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TextInputLayout
android:id="@+id/specialEditLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="@style/AppTheme.EditText.Input">
<com.santsg.paximum.ui.customview.component.ClearableEditText
android:id="@+id/specialEdt"
android:layout_width="match_parent"
android:layout_height="@dimen/clearable_edittext_input_height"/>
</android.support.design.widget.TextInputLayout>
和可清除的edittext,它位于特殊/自定义文本输入布局中。
ublic class ClearableEditText extends AppCompatEditText implements View.OnTouchListener, View.OnFocusChangeListener, TextWatcher {
private Drawable mClearTextIcon;
private OnFocusChangeListener mOnFocusChangeListener;
private OnTouchListener mOnTouchListener;
public ClearableEditText(final Context context) {
super(context);
init(context);
}
public ClearableEditText(final Context context, final AttributeSet attrs) {
super(context, attrs);
init(context);
}
public ClearableEditText(final Context context, final AttributeSet attrs, final int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
@Override
public void setOnFocusChangeListener(final OnFocusChangeListener onFocusChangeListener) {
mOnFocusChangeListener = onFocusChangeListener;
}
@Override
public void setOnTouchListener(final OnTouchListener onTouchListener) {
mOnTouchListener = onTouchListener;
}
private void init(final Context context) {
final Drawable drawable = ContextCompat.getDrawable(context, android.support.v7.appcompat.R.drawable.abc_ic_clear_material);
final Drawable wrappedDrawable = DrawableCompat.wrap(drawable); //Wrap the drawable so that it can be tinted pre Lollipop
DrawableCompat.setTint(wrappedDrawable, getCurrentHintTextColor());
mClearTextIcon = wrappedDrawable;
mClearTextIcon.setBounds(0, 0, mClearTextIcon.getIntrinsicHeight(), mClearTextIcon.getIntrinsicHeight());
setClearIconVisible(false);
super.setOnTouchListener(this);
super.setOnFocusChangeListener(this);
addTextChangedListener(this);
}
@Override
public void onFocusChange(final View view, final boolean hasFocus) {
if (hasFocus) {
setClearIconVisible(getText().length() > 0);
} else {
setClearIconVisible(false);
}
if (mOnFocusChangeListener != null) {
mOnFocusChangeListener.onFocusChange(view, hasFocus);
}
}
@Override
public boolean onTouch(final View view, final MotionEvent motionEvent) {
final int x = (int) motionEvent.getX();
if (mClearTextIcon.isVisible() && x > getWidth() - getPaddingRight() - mClearTextIcon.getIntrinsicWidth()) {
if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
setError(null);
setText("");
}
return true;
}
return mOnTouchListener != null && mOnTouchListener.onTouch(view, motionEvent);
}
@Override
public final void onTextChanged(final CharSequence s, final int start, final int before, final int count) {
if (isFocused()) {
setClearIconVisible(s.length() > 0);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
private void setClearIconVisible(final boolean visible) {
mClearTextIcon.setVisible(visible, false);
final Drawable[] compoundDrawables = getCompoundDrawables();
setCompoundDrawables(
compoundDrawables[0],
compoundDrawables[1],
visible ? mClearTextIcon : null,
compoundDrawables[3]);
}