生活随笔
收集整理的這篇文章主要介紹了
Button.setOnClickListener(OnClickListener l) 原理
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
【轉(zhuǎn)自】http://www.iteye.com/topic/635466
使用Button 可能都會監(jiān)聽之 當(dāng)其被點(diǎn)擊 就會有函數(shù)負(fù)責(zé)回調(diào) 那么其到底是怎么實(shí)現(xiàn)的呢?
?今天要做的就是摸清楚之 為了減少不必要的麻煩 打算extends EditText 并在其上設(shè)立監(jiān)聽器
??
[代碼 步驟]
1. 定義Edit2Text 且extends EditText
Java代碼??
public?class?Edit2Text?extends?EditText?{?? ????OnTextChangedListener?changedListener;?? ?????? ????TextWatcher?tWatcher;?? }??
??
2. 定義其上的監(jiān)聽器:OnTextChangedListener 并定義函數(shù):onChanged() 用于執(zhí)行具體回調(diào)
Java代碼??
public?static?interface?OnTextChangedListener?{?? ????????public?void?onChanged(Edit2Text?e2t,?String?text);?? ?????????? ????}??
?
Java代碼??
需要注意的?這幾行代碼的修飾關(guān)鍵字:?? ?? 1.?static?:使其能夠脫離Edit2Text而存在?? ?? 2.?interface?:?使其自動填充其內(nèi)部函數(shù)?? ?? 3.?“void?onChanged(Edit2Text?e2t,?String?text)”?中的第一個(gè)參數(shù)e2t?用于做分別?具體奧妙?后面再細(xì)說??
?
3.? 設(shè)定監(jiān)聽器
Java代碼??
public?void?setOnChangedListener(OnTextChangedListener?l){?? ????????changedListener?=?l;?? ????}??
??
4. 定義TextWatcher 當(dāng)字符內(nèi)容改變 通知監(jiān)聽器
?* 定義TextWatcher'
Java代碼??
tWatcher?=?new?TextWatcher(){?? ?? ????????????@Override?? ????????????public?void?afterTextChanged(Editable?s)?{?? ?????????????????? ?????????????????? ????????????}?? ?? ????????????@Override?? ????????????public?void?beforeTextChanged(CharSequence?s,?int?start,?int?count,?? ????????????????????int?after)?{?? ?????????????????? ?????????????????? ????????????}?? ?? ????????????@Override?? ????????????public?void?onTextChanged(CharSequence?s,?int?start,?int?before,?? ????????????????????int?count)?{?? ?????????????????? ????????????????updateText(s.toString());?? ????????????}?? ?????????????? ????????};?? ?????????? ????????this.addTextChangedListener(tWatcher);??
??
* 通知監(jiān)聽器
Java代碼??
private?void?updateText(String?s){?? ????????changedListener.onChanged(this,?s);?? ????}??
??
5. 如何使用
Java代碼??
public?class?Edit2TextTest?extends?Activity?{?? ?????? ????@Override?? ????public?void?onCreate(Bundle?savedInstanceState)?{?? ????????super.onCreate(savedInstanceState);?? ????????setContentView(R.layout.main);?? ?????????? ????????Edit2Text?e2t?=?new?Edit2Text(this);?? ????????setContentView(e2t);?? ?????????? ????????e2t.setOnChangedListener(new?Edit2Text.OnTextChangedListener(){?? ?? ????????????@Override?? ????????????public?void?onChanged(Edit2Text?e2t,?String?text)?{?? ?????????????????? ????????????????Log.d("TAG","[String:]"+text);?? ????????????}?? ?????????????? ????????});?? ????}?? }??
??
6. emulator 運(yùn)行截圖:
?*?
?
* 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!??
?
總結(jié)
以上是生活随笔為你收集整理的Button.setOnClickListener(OnClickListener l) 原理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。