什么都不说了,直接上代码
public class XZEditView extends EditText {
public XZEditView(Context context) {
super(context);
}
public XZEditView(Context context, AttributeSet attrs) {
super(context, attrs);
// setText(" ");
}
public XZEditView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public interface OnBackspacePressListener {
void onBackspacePressed();
}
private OnBackspacePressListener backspaceListener;
public void setOnBackspacePressListener(OnBackspacePressListener backspaceListener) {
this.backspaceListener = backspaceListener;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_DEL) {
if(backspaceListener != null) {
backspaceListener.onBackspacePressed();
}
}
return super.onKeyDown(keyCode, event);
}
@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
outAttrs.actionLabel = null;
outAttrs.inputType = InputType.TYPE_NULL;
BaseInputConnection connection = new BaseInputConnection(this, false) {
@Override
public String getTextBeforeCursor(int ignore, int ignore2) {
return " ";
}
};
return connection;
}
}
只需要实现OnBackspacePressListener 接口,就可以在onBackspacePressed方法中监听了