android :KeyListener这个接口在android.text.method中

 

它有如下子类:

 

BaseKeyListener, DateKeyListener, DateTimeKeyListener, 

DialerKeyListener, DigitsKeyListener, MultiTapKeyListener, 

NumberKeyListener, QwertyKeyListener, TextKeyListener, 

TimeKeyListener

今天要使用KeyListener来让EditText只能输入某些字符,如数字,大小写a~z等等

keylistener有如下函数:

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

里面有一个抽象函数:protected abstract char[] getAcceptedChars();

这个函数就是用来编写自己想要输入的字符数据:

比如:

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(){

                             ..........

                            把抽象函数实现

});

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐