«

Button.setOnClickListener(OnClickListener l) 原理

时间:2024-3-2 17:45     作者:韩俊     分类: Android


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);


}

复制代码

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


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


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


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

复制代码

  1. 设定监听器

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">

  1.    }  <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);

}

复制代码

  1. 如何使用

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">
  1. 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(&quot;TAG&quot;,&quot;[String:]&quot;&#43;text);  <br style="word-wrap:break-word">
    
        }  <br style="word-wrap:break-word">

});

 }  <br style="word-wrap:break-word">

}

复制代码

  • Log 信息:


Java代码 收藏代码

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

标签: android

热门推荐