在自定义了一个网格EditText后,起初允许完美,后来测试发现一个问题:在nexus系列手机上使用原始的键盘删除键完全不会响应、、、直接毁三观了。。。。。。
可能是android系统对键盘响应的处理上做了一些不为人知的设置,反正是不响应onKeyListener事件;通过几个小时的搜寻,在StackOverflow上终于找到了解决方案给个链接大家先可以自己瞅瞅:http://stackoverflow.com/questions/4886858/android-edittext-deletebackspace-key-event%22。。
具体的实现方法是通过InputConnectionWrapper
public class SecurityEditText extends EditText {
private OnDelKeyEventListener delKeyEventListener;
public SecurityEditText(Context context) {
super(context);
}
public SecurityEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SecurityEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
return new ZanyInputConnection(super.onCreateInputConnection(outAttrs),
true);
}
private class ZanyInputConnection extends InputConnectionWrapper {
public ZanyInputConnection(InputConnection target, boolean mutable) {
super(target, mutable);
}
@Override
public boolean sendKeyEvent(KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN
&& event.getKeyCode() == KeyEvent.KEYCODE_DEL) {
if (delKeyEventListener != null) {
delKeyEventListener.onDeleteClick();
return true;
}
}
return super.sendKeyEvent(event);
}
@Override
public boolean deleteSurroundingText(int beforeLength, int afterLength) {
if (beforeLength == 1 && afterLength == 0) {
return sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_DEL))
&& sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP,
KeyEvent.KEYCODE_DEL));
}
return super.deleteSurroundingText(beforeLength, afterLength);
}
}
/**
*
* 功能描述: <br>
* 〈功能详细描述〉
*
* @param delKeyEventListener EditText删除回调
*/
public void setDelKeyEventListener(OnDelKeyEventListener delKeyEventListener) {
this.delKeyEventListener = delKeyEventListener;
}
public interface OnDelKeyEventListener {
void onDeleteClick();
}
}
结合我的这篇 http://blog.csdn.net/yaya_soft/article/details/44307847 O(∩_∩)O哈哈~完美了