临时解决方案!EditTextErrorFixed.java
虽然这确实是一个 SDK 错误,但我已经设法使用反射方法让图标按预期工作。我检查了它是否适用于 4.2 和 4.2.1,并确认它适用于我更新的 Galaxy Nexus。
来源可以在这里找到。
EditTextErrorFixed
屏幕截图显示,即使焦点发生变化,底部的图标仍然存在。此外,它还包含另一个修复程序,如果用户在已经为空EditText
的情况下按 Delete,错误就会消失(另一个错误?)。
为方便起见,这里是EditTextErrorFixed
来源;该类可以很容易地在 XML 中使用:
package com.olegsv.showerrorfixeddemo;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.widget.EditText;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
* EditText which addresses issues with the error icon
* (http://stackoverflow.com/q/13756978/832776) and also the error icon
* disappearing on pressing delete in an empty EditText
*/
public class EditTextErrorFixed extends EditText {
public EditTextErrorFixed(Context context) {
super(context);
}
public EditTextErrorFixed(Context context, AttributeSet attrs) {
super(context, attrs);
}
public EditTextErrorFixed(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
/**
* Don't send delete key so edit text doesn't capture it and close error
*/
@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (TextUtils.isEmpty(getText().toString()) && keyCode == KeyEvent.KEYCODE_DEL)
return true;
else
return super.onKeyPreIme(keyCode, event);
}
/**
* Keep track of which icon we used last
*/
private Drawable lastErrorIcon = null;
/**
* Resolve an issue where the error icon is hidden under some cases in JB
* due to a bug http://code.google.com/p/android/issues/detail?id=40417
*/
@Override
public void setError(CharSequence error, Drawable icon) {
super.setError(error, icon);
lastErrorIcon = icon;
// if the error is not null, and we are in JB, force
// the error to show
if (error != null /* !isFocused() && */) {
showErrorIconHax(icon);
}
}
/**
* In onFocusChanged() we also have to reshow the error icon as the Editor
* hides it. Because Editor is a hidden class we need to cache the last used
* icon and use that
*/
@Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
super.onFocusChanged(focused, direction, previouslyFocusedRect);
showErrorIconHax(lastErrorIcon);
}
/**
* Use reflection to force the error icon to show. Dirty but resolves the
* issue in 4.2
*/
private void showErrorIconHax(Drawable icon) {
if (icon == null)
return;
// only for JB 4.2 and 4.2.1
if (android.os.Build.VERSION.SDK_INT != Build.VERSION_CODES.JELLY_BEAN &&
android.os.Build.VERSION.SDK_INT != Build.VERSION_CODES.JELLY_BEAN_MR1)
return;
try {
Class<?> textview = Class.forName("android.widget.TextView");
Field tEditor = textview.getDeclaredField("mEditor");
tEditor.setAccessible(true);
Class<?> editor = Class.forName("android.widget.Editor");
Method privateShowError = editor.getDeclaredMethod("setErrorIcon", Drawable.class);
privateShowError.setAccessible(true);
privateShowError.invoke(tEditor.get(this), icon);
} catch (Exception e) {
// e.printStackTrace(); // oh well, we tried
}
}
}