Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
423 views
in Technique[技术] by (71.8m points)

android - Unable to use drawable in EditText inside TextInputLayout

I recently upgraded Android Design library from 24.2.1 to 25.0.0. After this the "drawableX" feature in EditText doesn't work.

EDIT 01.11: I learned that setting drawable in xml works if you use android:drawableStart instead of android:drawableLeft. But setting setting drawables programatically does not work. I use this to make a "Clear"-button to empty the EditText. But this feature is broken now. I would appreciate any work-arounds or knowledge about if this is intentional from Google or a bug!

My code for Clearable edit-text that worked before but doesn't now:

public class ClearableErrorTextInputEditText extends ErrorTextInputEditText implements View.OnFocusChangeListener, View.OnTouchListener, TextWatcher {

private Drawable resIcon;
private OnFocusChangeListener childFocusListener;

public ClearableErrorTextInputEditText(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    setup();
}

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

public ClearableErrorTextInputEditText(Context context) {
    super(context);
    setup();
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_UP) {
        if (getCompoundDrawables()[2] != null) {
            final boolean tappedClose = event.getX() > (getWidth() - getPaddingRight() - resIcon.getIntrinsicWidth());
            if (tappedClose) {
                setText("");
                return false; // true will fail on emulator running 2.1 and physical keyboard / scroll wheel
            }
        }
    }
    return false;
}

@Override
public void setOnFocusChangeListener(OnFocusChangeListener l) {
    childFocusListener = l;
}

@Override
public void onFocusChange(View v, boolean hasFocus) {
    setClearIconVisible(hasFocus && getText().length() > 0);

    if (childFocusListener!=null){
        childFocusListener.onFocusChange(v, hasFocus);
    }
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count){
    super.onTextChanged(s, start, before, count);
    setClearIconVisible(isFocused() && s.length() > 0);
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {} // not interesting

@Override
public void afterTextChanged(Editable s) {} //  // not interesting

private void setup() {
    if(isInEditMode()){
        return;
    }

    resIcon = getResources().getDrawable(R.drawable.ic_clear, null);
    resIcon.setBounds(0, 0, resIcon.getIntrinsicWidth(), resIcon.getIntrinsicHeight());

    setClearIconVisible(false);

    super.setOnTouchListener(this);
    super.setOnFocusChangeListener(this);
    addTextChangedListener(this);
}

private void setClearIconVisible(final boolean visible){
    final Drawable icon = visible ? resIcon : null;
    setCompoundDrawables(getCompoundDrawables()[0],
            getCompoundDrawables()[1], icon, getCompoundDrawables()[3]);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is the easiest way I could find:

Step 1.

On your layout, set your endIconDrawable, It's important to know that it will not show if you don't set the endIconMode.

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/custom_end_icon"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/hint_text"
    app:endIconMode="custom"
    app:endIconDrawable="@drawable/custom_icon"
    app:endIconContentDescription="@string/custom_content_desc">

  <com.google.android.material.textfield.TextInputEditText
      android:layout_width="match_parent"
      android:layout_height="wrap_content"/>

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

Step 2. Depending on what you want to do with the button, these are the three methods you can call on it

TextInputLayout textInputCustomEndIcon = view.findViewById(R.id.custom_end_icon);

// If the icon should work as button, set an OnClickListener to it.
textInputCustomEndIcon
    .setEndIconOnClickListener(/* custom OnClickListener */);

// If any specific changes should be done when the EditText is attached (and
// thus when the end icon is added to it), set an OnEditTextAttachedListener
textInputCustomEndIcon
    .addOnEditTextAttachedListener(/* custom OnEditTextAttachedListener */);

// If any specific changes should be done if/when the endIconMode gets changed,
// set an OnEndIconChangedListener
textInputCustomEndIcon
    .addOnEndIconChangedListener(/* custom OnEndIconChangedListener */);

For more on Customization, and additional features check this Link cheers


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...