Java, Android custom listener Interface

Source: Internet
Author: User

The Android system implements the onclicklistener and ontouchlistener interfaces. However, if you want your application to respond to custom input events, you must implement a custom listener interface.

This post provides a good example. I will post it here.

Myactivity. Java

public class MyActivity extends Activity implements InternetManager.Listener {    private TextView mText;    private InternetManager mInetMgr;    /* called just like onCreate at some point in time */     public void onStateChange(boolean state) {        if (state) {            mText.setText("on");        } else {            mText.setText("off");        }    }    public void onCreate() {        mInetMgr = new InternetManager();        mInetMgr.registerListener(this);        mInetMgr.doYourWork();    }}

Internetmanager. Java

public class InternetManager {    // all the listener stuff below    public interface Listener {        public void onStateChange(boolean state);    }    private Listener mListener = null;    public void registerListener (Listener listener) {        mListener = listener;    }    // -----------------------------    // the part that this class does    private boolean isInternetOn = false;    public void doYourWork() {        // do things here        // at some point        isInternetOn = true;        // now notify if someone is interested.        if (mListener != null)            mListener.onStateChange(isInternetOn);    }}

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.