zoukankan      html  css  js  c++  java
  • TextWatcher

     

    对于一些需求,如非法字符限制(例如不允许输入#号,如果输入了#给出错误提示),做成动态判断更方便一些,而且容易扩展;

         在Android里使用TextWatcher接口可以很方便的对EditText进行监听;TextWatcher中有3个函数需要重载:

        public void beforeTextChanged(CharSequence s, int start,
                                      int count, int after);
        public void onTextChanged(CharSequence s, int start, int before, int count);
        public void afterTextChanged(Editable s);

         从函数名就可以知道其意思,每当敲击键盘编辑框的文字改变时,上面的三个函数都会执行,beforeTextChanged可以给出变化之前的内容,onTextChanged和afterTextChanged给出追加上新的字符之后的文本;

    [java] view plaincopy
     
    1. classimplements         * beginning at start are about to be replaced by new text with length after. 
    2.         * 在s中,从start处开始的count个字符将要被长度为after的文本替代 
    3.         * s 为变化前的内容; 
    4.         * start 为开始变化位置的索引,从0开始计数; 
    5.         * count 为将要发生变化的字符数 
    6.         * after 为用来替换旧文本的长度,比如s由1变为12,after为1,由12变为1,after为0; 
    7.         */   
    8. publicvoidintint int   
    9. +s++start++count++after);  
    10.         * This method is called to notify you that, within s, the count characters 
    11.         *  beginning at start have just replaced old text that had length before 
    12.         *  在s中,从start处开始的count个字符刚刚替换了原来长度为before的文本 
    13.         *  s 为变化后的内容; 
    14.         *  start 为开始变化位置的索引,从0开始计数; 
    15.         *  before 为被取代的老文本的长度,比如s由1变为12,before为0,由12变为1,before为1; 
    16.         *  count 为将要发生变化的字符数 
    17.         */   
    18. publicvoidintint int   
    19. +s++start++before++count);  
    20.         * This method is called to notify you that, somewhere within s, the text has been changed. 
    21.         */   
    22. publicvoid   
    23. +s);  
    24. }  


         注册监听:

    EditText mEditor = (EditText)findViewById(R.id.editor_input);
    mEditor.addTextChangedListener(mTextWatcher);
  • 相关阅读:
    SPSS—回归—曲线估计方程案例解析
    SPSS—非线性回归(模型表达式)案例解析
    SPSS—回归—二元Logistic回归案例分析
    SPSS-两变量相关性分析
    SPSS-单因素方差分析(ANOVA) 案例解析
    SPSS-比较均值-独立样本T检验 案例解析
    做MFC的时候引用了头文件但VC还是报missing storage-class or type specifiers
    GetDocument missing storage-class or type specifiers的解决方法
    Python如何安装模块
    AE IRasterCursor 改变栅格图层像素值
  • 原文地址:https://www.cnblogs.com/xgjblog/p/3878755.html
Copyright © 2011-2022 走看看