什么都不說了富弦,直接上代碼
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;
}
}
只需要實現(xiàn)OnBackspacePressListener 接口癞揉,就可以在onBackspacePressed方法中監(jiān)聽了