美文网首页
Android KeyListener

Android KeyListener

作者: hamsik2046 | 来源:发表于2015-06-11 13:16 被阅读1278次

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

    ..........

    把抽象函数实现

    });

    相关文章

      网友评论

          本文标题:Android KeyListener

          本文链接:https://www.haomeiwen.com/subject/yyejqttx.html