Android App-Custom listener 2

Source: Internet
Author: User

Now let's talk about the second method of customizing the listener.


1. Create a new application

2, the definition interface spy, the same article.

Package Com.test.leetlelistener2;public interface Spy {public void Listening ();}

3. Define Class Enemy

Package Com.test.leetlelistener2;public class Enemy {public Spy spy;public int time = 10; Timer timer=new timer ();p ublic void Setspy (Spy s) {this.spy = S;timer.start ();} /** * Definition Timer * @author LEE * */class Timer extends Thread {@Overridepublic void run () {super.run (); while (time >= 0) {Ti me--;try {thread.sleep;} catch (Interruptedexception e) {e.printstacktrace ()}} Spy. Listening ();}}}

4, defines the listener class leader, it replaces the previous method the mainactivity implements the interface spy

Package Com.test.leetlelistener2;public class Leader implements spy{@Overridepublic void Listening () { System.out.println ("Spyclass Listening ()");}}

5, Mainactivity

Package com.test.leetlelistener2;import android.os.bundle;import android.app.activity;import  android.view.Menu;public class MainActivity extends Activity {      @Override     protected void oncreate (bundle savedinstancestate)  {        super.oncreate (savedinstancestate);         setcontentview (R.layout.activity_main);          enemy enemy=new enemy ();         Enemy.setspy (New leader ());      }     @Override      public boolean oncreateoptionsmenu (Menu menu)  {         getmenuinflater (). Inflate (R.menu.main, menu);         return true;    }    } 

6, operation, 10s after Logcat can be viewed to have

Spyclass Listening ()

Output, this I did not write the process of closing the program, mainly to look at the use of other classes to implement the Spy interface effect.


So, in contrast to the two examples, you can see what points you need to be aware of from the definition listener.

Here's a summary:

1. What events should be monitored? (Interface spy and its method, the event to be monitored in this example is the end of the 10s timing)

2. Who is the object being monitored? (Enemy, an event to be monitored--10s)

3. Who is the recipient of the listener event? Who will be reported to the listener after the event has occurred? (Implements spy class, leader in this article, mainactivity in the previous article)

4. When and where to set the listener? (This depends on the case, because it is only an example, so I have been directly at the beginning of the program set up the monitoring and start timer, etc., in the actual programming process, whether it is to set the monitoring or listen to the event after the notification to listeners, generally have conditions/trigger event. In addition, sometimes the listener needs to pass some parameters, this time can be done by the interface spy in the method to set parameters to achieve such operations. In addition, there is a similar state of monitoring, we may need to use a string or a number to indicate a particular situation or state. For example, in the repeatedly fancy, when the game state has changed, need the listener to pass a parameter to indicate the game state is-win, timeout, etc. We may define these two states as 1 and 2.1 to win, 2 to time out, but to do so, the later changes will be more difficult, such as I want to add a state, not timed out but no way to go, so add a 3, plus a good, if there are other, and then to change the more bother. Later, my approach is to define some static variables in the spy interface to represent the state of the game, each time as long as the variable name of the semantic settings can be, and the modification will be more convenient. Could it be stupid to handle this? But it also solves some of the problems, at least if I read or revise the code I wrote six months later, it won't be too difficult. )

Like this:

Package Com.test.leetlelistener2;public interface Spy {public static int game_win=1;public static int game_overtime=2; public static int game_die=3;public void Listening (int gamestatus);}

Add a parameter to the previous code, listening definition and call.

Custom Listener 1 to this end! Attach the source code .

2014.12.30

Li Yengiang

Android App-Custom listener 2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.