我最近将 Android 设计库从 24.2.1 升级到了 25.0.0.在此之后 EditText 中的drawableX"功能不起作用.
I recently upgraded Android Design library from 24.2.1 to 25.0.0. After this the "drawableX" feature in EditText doesn't work.
编辑 01.11:我了解到,如果您使用 android:drawableStart 而不是 android:drawableLeft,则在 xml 中设置 drawable 有效.但是以编程方式设置设置drawables不起作用.我用它来制作一个清除"按钮来清空 EditText.但是这个功能现在被打破了.如果这是来自 Google 的故意或错误,我将不胜感激任何解决方法或知识!
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]);
}
根据 Ahmed Ashraf G 的回答,我找到了解决方案.更改以下代码:
Based on the answer by Ahmed Ashraf G, I was able to find the solution. Changing the following code:
setCompoundDrawables(getCompoundDrawables()[0],
getCompoundDrawables()[1], icon, getCompoundDrawables()[3]);
到:
setCompoundDrawablesRelative(getCompoundDrawablesRelative()[0],
getCompoundDrawablesRelative()[1], icon, getCompoundDrawablesRelative()[3]);
从 StackOverflow 上的其他地方(文档没有提到这一点),xxxCompoundDrawablesXxx 和 xxxCompundDrawablesRelativeXxx 之间的区别在于相对版本考虑了 RTL 语言,即它们与使用 drawableStart 而不是 drawableLeft 相同.
From other places on StackOverflow (the documentation does NOT mention this) the difference between xxxCompoundDrawablesXxx and xxxCompundDrawablesRelativeXxx is that the relative versions take into account RTL-languages, ie they are the same as using drawableStart instead of drawableLeft.
因此,总而言之,Google 已经破坏了 TextInputLayout 内 EditText 的所有不是 RTL 方法的方法.由于新方法是在 API 级别 17 中添加的,我认为这是主要错误,可能会在未来的更新中修复
So in conclusion, Google has broken all methods that are not RTL-methods for EditText inside TextInputLayout. Since the new methods are added in API level 17 I see this as major bug, that will probably be fixed in future updates
这篇关于无法在 TextInputLayout 内的 EditText 中使用 drawable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!