Android 自定义监听器

CustomListenerInterface.java

public interface CustomListenerInterface {
    // Update the data
    public void updateData();
}

MainActivity.java

public class MainActivity extends AppCompatActivity {
    private Button startListen = null;
    private Button endListen = null;
    private volatile boolean statue = false;

    private CustomListenerHandle listenerHandle = new CustomListenerHandle();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        listenerHandle.setCustomListener(new CustomListenerInterface() {
            @Override
            public void updateData() {
                Log.d("**CustomListener**","----数据变化----");
            }
        });

        startListen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("**CustomListener**","--------启动监听--------");
                statue = true;
                // Start the thread
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        int i=0;
                        while (statue){
                            i++;
                            if(i == 1000000){
                                listenerHandle.handlerData();
                                i = 0;
                            }
                        }
                    }
                }).start();
            }
        });

        endListen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                statue = false;
                Log.d("**CustomListener**","--------停止监听--------");
            }
        });

    }

    private void init() {
        this.startListen = findViewById(R.id.start_listen);
        this.endListen = findViewById(R.id.end_listen);
    }

    public class CustomListenerHandle {
        private CustomListenerInterface customListenerInterface = null;

        //设置监听器
        public void setCustomListener(CustomListenerInterface customListenerInterface){
            this.customListenerInterface = customListenerInterface;
        }

        //处理程序数据
        public void handlerData(){
            this.customListenerInterface.updateData();
        }
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:gravity="center_horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="自定义监听事件,监听数字的变化"/>

    <Button
        android:id="@+id/start_listen"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="开始监听"/>

    <Button
        android:id="@+id/end_listen"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="停止监听"/>

</LinearLayout>

源码:
链接:https://pan.baidu.com/s/1sLrJrC3mH6_OWt46TpX6wQ
提取码:gzl5

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王睿丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值