android,java使用Button 可能都会监听之 当其被点击 就会有函数负责回调 那么其到底是怎么实现的呢?
今天要做的就是摸清楚之 为了减少不必要的麻烦 打算extends EditText 并在其上设立监听器
[代码 步骤]
1. 定义Edit2Text 且extends EditText
public class Edit2Text extends EditText {  
OnTextChangedListener changedListener;  
TextWatcher tWatcher;  
}  
复制代码
2. 定义其上的监听器:OnTextChangedListener 并定义函数:onChanged() 用于执行具体回调
public static interface OnTextChangedListener {  
public void onChanged(Edit2Text e2t, String text);  
}  
复制代码
需要注意的 这几行代码的修饰关键字:  
- static :使其能够脱离Edit2Text而存在  
- interface : 使其自动填充其内部函数  
- “void onChanged(Edit2Text e2t, String text)” 中的第一个参数e2t 用于做分别 具体奥妙 后面再细说  
复制代码
- 设定监听器
public void setOnChangedListener(OnTextChangedListener l){  
changedListener = l;  
}  
复制代码
4. 定义TextWatcher 当字符内容改变 通知监听器
* 定义TextWatcher'
tWatcher = new TextWatcher(){  
       @Override  <br style="word-wrap:break-word">
        public void afterTextChanged(Editable s) {  <br style="word-wrap:break-word">
          // TODO Auto-generated method stub  <br style="word-wrap:break-word">- 
} <br style="word-wrap:break-word">
        @Override  <br style="word-wrap:break-word">
        public void beforeTextChanged(CharSequence s, int start, int count,  <br style="word-wrap:break-word">
               int after) {  <br style="word-wrap:break-word">
        // TODO Auto-generated method stub  <br style="word-wrap:break-word">
           <br style="word-wrap:break-word">
      }  <br style="word-wrap:break-word">        @Override  <br style="word-wrap:break-word">
      public void onTextChanged(CharSequence s, int start, int before,  <br style="word-wrap:break-word">
            int count) {  <br style="word-wrap:break-word">
           // TODO Auto-generated method stub  <br style="word-wrap:break-word">
          updateText(s.toString());  <br style="word-wrap:break-word">
         }  <br style="word-wrap:break-word">
         <br style="word-wrap:break-word">
   };  <br style="word-wrap:break-word">
      <br style="word-wrap:break-word">this.addTextChangedListener(tWatcher);  
复制代码
- 通知监听器
private void updateText(String s){  
changedListener.onChanged(this, s);  
} 
复制代码
- 如何使用
public class Edit2TextTest extends Activity {  
 /** Called when the activity is first created. */  <br style="word-wrap:break-word">@Override  
public void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  <br style="word-wrap:break-word">- 
setContentView(R.layout.main); Edit2Text e2t = new Edit2Text(this); setContentView(e2t); <br style="word-wrap:break-word"> <br style="word-wrap:break-word"> e2t.setOnChangedListener(new Edit2Text.OnTextChangedListener(){ <br style="word-wrap:break-word"> @Override <br style="word-wrap:break-word"> public void onChanged(Edit2Text e2t, String text) { <br style="word-wrap:break-word"> // TODO Auto-generated method stub <br style="word-wrap:break-word"> Log.d("TAG","[String:]"+text); <br style="word-wrap:break-word"> } <br style="word-wrap:break-word">
});  
 }  <br style="word-wrap:break-word">} 
复制代码
- Log 信息:
Java代码  收藏代码
- 
D/dalvikvm( 674): GC freed 223 objects / 8848 bytes in 108m 
- 
D/TAG ( 941): [String:]i am 
- 
D/TAG ( 941): [String:]i am 
- 
D/TAG ( 941): [String:]i am e 
- 
D/TAG ( 941): [String:]i am ed 
- 
D/TAG ( 941): [String:]i am edi 
- 
D/TAG ( 941): [String:]i am edit 
- 
D/TAG ( 941): [String:]i am edit2 
- 
D/TAG ( 941): [String:]i am edit2t 
- 
D/TAG ( 941): [String:]i am edit2te 
- 
D/TAG ( 941): [String:]i am edit2tex 
- 
D/TAG ( 941): [String:]i am edit2text 
- 
D/TAG ( 941): [String:]i am edit2text, 
- 
D/TAG ( 941): [String:]i am edit2text, 
- 
D/TAG ( 941): [String:]i am edit2text, h 
- 
D/TAG ( 941): [String:]i am edit2text, he 
- 
D/TAG ( 941): [String:]i am edit2text, hel 
- 
D/TAG ( 941): [String:]i am edit2text, hell 
- 
D/TAG ( 941): [String:]i am edit2text, hello 
- 
D/TAG ( 941): [String:]i am edit2text, hello!