Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Button.setOnClickListener (OnClickListener l) principle

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Android,java may listen when it is clicked using Button, and there will be a function responsible for callback when it is clicked, so how exactly is it implemented?

What we need to do today is to figure out how to prepare extends EditText and set up listeners on it in order to reduce unnecessary trouble.

[code steps]

1. Define Edit2Text and extends EditText

Public class Edit2Text extends EditText {

OnTextChangedListener changedListener

TextWatcher tWatcher

}

Copy the code

two。 Define the listener on it: OnTextChangedListener and define the function: onChanged () to perform a specific callback

Public static interface OnTextChangedListener {

Public void onChanged (Edit2Text e2t, String text)

}

Copy the code

Note the decorating keywords for these lines of code:

1. Static: enable it to exist without Edit2Text

2. Interface: make it automatically populate its internal functions

3. The first parameter e2t in "void onChanged (Edit2Text e2t, String text)" is used to do specific details later.

Copy the code

3. Set the listener

Public void setOnChangedListener (OnTextChangedListener l) {

ChangedListener = l

}

Copy the code

4. Define TextWatcher to notify listeners when character content changes

* define 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)

Copy the code

* notify the listener

Private void updateText (String s) {

ChangedListener.onChanged (this, s)

}

Copy the code

5. How to use

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)

}

});

}

}

Copy the code

* Log information:

Java Code Collection Code

01. D/dalvikvm: GC freed 223 objects / 8848 bytes in 108m

02. D/TAG: [String:] I am

03. D/TAG: [String:] I am

04. D/TAG: [String:] i am e

05. D/TAG: [String:] i am ed

06. D/TAG: [String:] i am edi

07. D/TAG: [String:] i am edit

08. D/TAG: [String:] I am edit2

09. D/TAG: [String:] I am edit2t

10. D/TAG: [String:] I am edit2te

11. D/TAG: [String:] I am edit2tex

12. D/TAG: [String:] I am edit2text

13. D/TAG: [String:] I am edit2text

14. D/TAG: [String:] I am edit2text

15. D/TAG: [String:] I am edit2text, h

16. D/TAG: [String:] I am edit2text, he

17. D/TAG: [String:] I am edit2text, hel

18. D/TAG: [String:] I am edit2text, hell

19. D/TAG: [String:] I am edit2text, hello

20. D/TAG: [String:] I am edit2text, hello!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report