Android自帶了很多監(jiān)聽(tīng)器,比如OnClickListener。實(shí)現(xiàn)一個(gè)自定義Listener也是很簡(jiǎn)單的,只需要用一個(gè)Interface接口
創(chuàng)新互聯(lián)建站是一家專注于做網(wǎng)站、網(wǎng)站建設(shè)與策劃設(shè)計(jì),渾南網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)建站做網(wǎng)站,專注于網(wǎng)站建設(shè)10多年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:渾南等地區(qū)。渾南做網(wǎng)站價(jià)格咨詢:13518219792
public interface Listener { public void update(String string); }
public class Operater { private Listener mListener; public void setListener(Listener listener) { mListener = listener; } public void doSomething(String string) { if (mListener != null) { mListener.update(string); } } }
public class Main { public static void main(String[] args) { Operater op = new Operater(); op.setListener(new Listener() { public void update(String string) { System.out.println("監(jiān)聽(tīng)器:"+string); } }); op.doSomething("已經(jīng)點(diǎn)擊了"); } }
監(jiān)聽(tīng)器:已經(jīng)點(diǎn)擊了
以上用Java的簡(jiǎn)單方式實(shí)現(xiàn)了在Android中經(jīng)常用到的Listener