Android Custom Callbacks

Source: Internet
Author: User

When we first approached, I felt that the callback was a difficult concept to understand, and then we kept on surfing the internet to find the definition of the callback: (When you want to put the specific action, let the specific person to complete, you define an interface is good.) When using the interface, the specific person to implement your interface, that is, the implementation of the callback. )

In fact, a careful thought, the OnClick (view view) is a kind of callback, only understand the essence of the principle of things, can be used flexibly.

An example of a custom interface in dialog is given below:

1, the new Xdialog inherit dialog,

2. Define the interface

 Public Interface callbacklistener{        void  testcall ();         void Testcall (Object o);    }

3. Defining variables

Private Callbacklistener Callbacklistener;  Public void Setcallbacklistener (Callbacklistener callbacklistener) {        this. Callbacklistener=  Callbacklistener;    }

4, in the construction method:

 Private intCount = 0; Private voidInitview (Context context) {button Button=(Button) Findviewbyid (R.ID.TEST_BTN); Button.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {if(NULL!=Callbacklistener) {                    ++count;                Callbacklistener.testcall (count);    }            }        }); }

5. Implement callbacks in Mainactivity:

Callbackdialog Mcallback =NewCallbackdialog (mainactivity. This);//Implementing CallbacksMcallback.setcallbacklistener (NewCallbackdialog.callbacklistener () {@Override Public voidTestcall () {} @Override Public voidTestcall (Object o) {//mtextview.settext (o.tostring ());
DoSomething ();
} });

Android Custom Callbacks

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.