Just extend EditText:
public class EditTextV2 extends EditText
{
public EditTextV2( Context context )
{
super( context );
}
public EditTextV2( Context context, AttributeSet attribute_set )
{
super( context, attribute_set );
}
public EditTextV2( Context context, AttributeSet attribute_set, int def_style_attribute )
{
super( context, attribute_set, def_style_attribute );
}
@Override
public boolean onKeyPreIme( int key_code, KeyEvent event )
{
if ( key_code == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP )
this.clearFocus();
return super.onKeyPreIme( key_code, event );
}
}
And in the xml just use <yourPackage.EditTextV2>
instead of <EditText>
.
Note: You may need to add/remove constructors to this class depending on the min API you're supporting. I suggest just adding them all and removing the ones whose super()
calls get underlined in red.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…