你需要在其中制作Custom EditText,Make Extends EditText和@Override Below Method。
这是示例代码。
private ArrayList keysToIgnore = new ArrayList();
public EditTextCus(Context context) {
super(context);
}
public EditTextCus(Context context, AttributeSet attrs){
super(context, attrs);
}
public EditTextCus(Context context, AttributeSet attrs, int defStyle){
super (context, attrs, defStyle);
}
private Boolean keyInIgnoreList(int keyCode) {
for (Integer i:keysToIgnore){
int key = i.intValue();
if (key == keyCode)
return true;
}
return false;
}
public void addKeyToIgnoreList(int keyCode) {
if (!keyInIgnoreList(keyCode)){
keysToIgnore.add(keyCode);
}
}
public void removeKeyFromIgnoreList(int keyCode) {
if (keyInIgnoreList(keyCode)){
Integer key=new Integer(keyCode);
keysToIgnore.remove(key);
}
}
@Override
public boolean dispatchKeyEventPreIme(KeyEvent event) {
//Log.d("ws", "dispatchKeyEventPreIme(" + event + ")");
// swallow the any key in the ignore list
if (keyInIgnoreList(event.getKeyCode())) {
KeyEvent.DispatcherState state = getKeyDispatcherState();
if (state != null) {
if (event.getAction() == KeyEvent.ACTION_DOWN
/*&& event.getRepeatCount() == 0*/) {
state.startTracking(event, this);
return true;
} else if (event.getAction() == KeyEvent.ACTION_UP
&& !event.isCanceled() && state.isTracking(event)) {
return true;
}
}
}
}
return super.dispatchKeyEventPreIme(event);
}
你也可以这样做搜索键:
mCustomEditText.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId==EditorInfo.IME_ACTION_SEARCH && event.getAction()==KeyEvent.KEYCODE_SEARCH){
System.out.println("Search pressed.........");
}
return false;
}
});