android,java使用Button 可能都會(huì)監(jiān)聽(tīng)之 當(dāng)其被點(diǎn)擊 就會(huì)有函數(shù)負(fù)責(zé)回調(diào) 那么其到底是怎么實(shí)現(xiàn)的呢? 創(chuàng)新互聯(lián)是一家以網(wǎng)站建設(shè)公司、網(wǎng)頁(yè)設(shè)計(jì)、品牌設(shè)計(jì)、軟件運(yùn)維、成都網(wǎng)站營(yíng)銷、小程序App開(kāi)發(fā)等移動(dòng)開(kāi)發(fā)為一體互聯(lián)網(wǎng)公司。已累計(jì)為輕質(zhì)隔墻板等眾行業(yè)中小客戶提供優(yōu)質(zhì)的互聯(lián)網(wǎng)建站和軟件開(kāi)發(fā)服務(wù)。 今天要做的就是摸清楚之 為了減少不必要的麻煩 打算extends EditText 并在其上設(shè)立監(jiān)聽(tīng)器
[代碼 步驟] 1. 定義Edit2Text 且extends EditText
public class Edit2Text extends EditText { OnTextChangedListener changedListener; TextWatcher tWatcher; }
復(fù)制代碼
2. 定義其上的監(jiān)聽(tīng)器:OnTextChangedListener 并定義函數(shù):onChanged() 用于執(zhí)行具體回調(diào) public static interface OnTextChangedListener { public void onChanged(Edit2Text e2t, String text); }
復(fù)制代碼 需要注意的 這幾行代碼的修飾關(guān)鍵字: 1. static :使其能夠脫離Edit2Text而存在 2. interface : 使其自動(dòng)填充其內(nèi)部函數(shù) 3. “void onChanged(Edit2Text e2t, String text)” 中的第一個(gè)參數(shù)e2t 用于做分別 具體奧妙 后面再細(xì)說(shuō)
復(fù)制代碼 3. 設(shè)定監(jiān)聽(tīng)器public void setOnChangedListener(OnTextChangedListener l){ changedListener = l; }
復(fù)制代碼 4. 定義TextWatcher 當(dāng)字符內(nèi)容改變 通知監(jiān)聽(tīng)器 * 定義TextWatcher' tWatcher = new TextWatcher(){ @Override public void afterTextChanged(Editable s) { // TODO Auto-generated method stub 06. } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub updateText(s.toString()); } }; this.addTextChangedListener(tWatcher);
復(fù)制代碼 * 通知監(jiān)聽(tīng)器private void updateText(String s){ changedListener.onChanged(this, s); }
復(fù)制代碼 5. 如何使用public class Edit2TextTest extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); 06. setContentView(R.layout.main); Edit2Text e2t = new Edit2Text(this); setContentView(e2t); e2t.setOnChangedListener(new Edit2Text.OnTextChangedListener(){ @Override public void onChanged(Edit2Text e2t, String text) { // TODO Auto-generated method stub Log.d("TAG","[String:]"+text); } }); } }
復(fù)制代碼 * Log 信息:Java代碼 收藏代碼 01. D/dalvikvm( 674): GC freed 223 objects / 8848 bytes in 108m 02. D/TAG ( 941): [String:]i am 03. D/TAG ( 941): [String:]i am 04. D/TAG ( 941): [String:]i am e 05. D/TAG ( 941): [String:]i am ed 06. D/TAG ( 941): [String:]i am edi 07. D/TAG ( 941): [String:]i am edit 08. D/TAG ( 941): [String:]i am edit2 09. 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!
|