android,java使用Button 可能都会监听之 当其被点击 就会有函数负责回调 那么其到底是怎么实现的呢?

今天要做的就是摸清楚之 为了减少不必要的麻烦 打算extends EditText 并在其上设立监听器


[代码 步骤]

1. 定义Edit2Text 且extends EditText


  1. public class Edit2Text extends EditText {  

  2. OnTextChangedListener changedListener;  


  3. TextWatcher tWatcher;  

  4. }  


复制代码


2. 定义其上的监听器:OnTextChangedListener 并定义函数:onChanged() 用于执行具体回调

  1. public static interface OnTextChangedListener {  

  2. public void onChanged(Edit2Text e2t, String text);  


  3. }  


复制代码

  1. 需要注意的 这几行代码的修饰关键字:  


  2. 1. static :使其能够脱离Edit2Text而存在  


  3. 2. interface : 使其自动填充其内部函数  


  4. 3. “void onChanged(Edit2Text e2t, String text)” 中的第一个参数e2t 用于做分别 具体奥妙 后面再细说  


复制代码

3.  设定监听器
  1. public void setOnChangedListener(OnTextChangedListener l){  

  2. changedListener = l;  

  3. }  


复制代码

4. 定义TextWatcher 当字符内容改变 通知监听器

* 定义TextWatcher'

  1. tWatcher = new TextWatcher(){  

  2.            @Override  

  3.             public void afterTextChanged(Editable s) {  

  4.               // TODO Auto-generated method stub  

  5. 06.                   

  6.            }  


  7.             @Override  

  8.             public void beforeTextChanged(CharSequence s, int start, int count,  

  9.                    int after) {  

  10.             // TODO Auto-generated method stub  

  11.                

  12.           }  


  13.             @Override  

  14.           public void onTextChanged(CharSequence s, int start, int before,  

  15.                 int count) {  

  16.                // TODO Auto-generated method stub  

  17.               updateText(s.toString());  

  18.              }  

  19.              

  20.        };  

  21.           

  22.   this.addTextChangedListener(tWatcher);  


复制代码

* 通知监听器
  1. private void updateText(String s){  

  2. changedListener.onChanged(this, s);  



复制代码

5. 如何使用
  1. public class Edit2TextTest extends Activity {  

  2.      /** Called when the activity is first created. */  

  3. @Override  

  4.    public void onCreate(Bundle savedInstanceState) {  

  5.          super.onCreate(savedInstanceState);  

  6. 06.         setContentView(R.layout.main);         

  7.        Edit2Text e2t = new Edit2Text(this);  

  8.         setContentView(e2t);  

  9.            

  10.         e2t.setOnChangedListener(new Edit2Text.OnTextChangedListener(){     

  11.             @Override  

  12.            public void onChanged(Edit2Text e2t, String text) {  

  13.                // TODO Auto-generated method stub  

  14.                Log.d("TAG","[String:]"+text);  

  15.             }  

  16. });  

  17.      }  



复制代码

* Log 信息:

  1. Java代码  收藏代码

  2. 01. D/dalvikvm(  674): GC freed 223 objects / 8848 bytes in 108m  

  3. 02. D/TAG     (  941): [String:]i am  

  4. 03. D/TAG     (  941): [String:]i am  

  5. 04. D/TAG     (  941): [String:]i am e  

  6. 05. D/TAG     (  941): [String:]i am ed  

  7. 06. D/TAG     (  941): [String:]i am edi  

  8. 07. D/TAG     (  941): [String:]i am edit  

  9. 08. D/TAG     (  941): [String:]i am edit2  

  10. 09. D/TAG     (  941): [String:]i am edit2t  

  11. 10. D/TAG     (  941): [String:]i am edit2te  

  12. 11. D/TAG     (  941): [String:]i am edit2tex  

  13. 12. D/TAG     (  941): [String:]i am edit2text  

  14. 13. D/TAG     (  941): [String:]i am edit2text,  

  15. 14. D/TAG     (  941): [String:]i am edit2text,  

  16. 15. D/TAG     (  941): [String:]i am edit2text, h  

  17. 16. D/TAG     (  941): [String:]i am edit2text, he  

  18. 17. D/TAG     (  941): [String:]i am edit2text, hel  

  19. 18. D/TAG     (  941): [String:]i am edit2text, hell  

  20. 19. D/TAG     (  941): [String:]i am edit2text, hello  

  21. 20. D/TAG     (  941): [String:]i am edit2text, hello!