android :KeyListener這個(gè)接口在android.text.method中
它有如下子類:
BaseKeyListener,DateKeyListener,DateTimeKeyListener,
DialerKeyListener,DigitsKeyListener,MultiTapKeyListener,
NumberKeyListener,QwertyKeyListener,TextKeyListener,
今天要使用KeyListener來(lái)讓EditText只能輸入某些字符芯急,如數(shù)字揖曾,大小寫a~z等等
keylistener有如下函數(shù):
public int getInputType();
public boolean onKeyDown(View view, Editable text,
int keyCode, KeyEvent event);
public boolean onKeyUp(View view, Editable text,
int keyCode, KeyEvent event);
public boolean onKeyOther(View view, Editable text, KeyEvent event);
public void clearMetaKeyState(View view, Editable content, int states);
NumberKeyListener定義
public abstract class NumberKeyListener extends BaseKeyListener implements InputFilter
里面有一個(gè)抽象函數(shù):protected abstract char[] getAcceptedChars();
這個(gè)函數(shù)就是用來(lái)編寫自己想要輸入的字符數(shù)據(jù):
比如:
public char [] getAcceptedChars(){
char numberChars[] ={'0'? ,?'1' ,'2' ,' 3' , ' 4' , '5'? ,'6'? ,' 7?' ,? '8'? , '9'};
return numberChars;
}
public char [] getAcceptedChars(){
char [] myChar ={'a','b','c','d','e','f','g','h','i'.......};
return myChar;
}
editText.setKeyListener(new numberKeyListener(){
..........
把抽象函數(shù)實(shí)現(xiàn)
});