只需扩展 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 );
}
}
而在 xml 中,只需使用<yourPackage.EditTextV2>
而不是<EditText>
.
注意:您可能需要向此类添加/删除构造函数,具体取决于您支持的最小 API。我建议将它们全部添加并删除那些super()
调用带有红色下划线的那些。