欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Handler实现计数器

程序员文章站 2022-06-28 13:46:29
xml

xml

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

    <TextView
        android:id="@+id/tv_demo_number"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="10"
        android:textColor="#ff0000"
        android:textSize="20sp"/>

    <Button
        android:id="@+id/btn_demo_increase"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="自动增加" />

    <Button
        android:id="@+id/btn_demo_decrease"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="自动减少" />

    <Button
        android:id="@+id/btn_demo_pause"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="暂停"
        android:enabled="false"/>

</LinearLayout>

activity

package com.example.handlerdemo;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class HandlerDemo extends AppCompatActivity implements View.OnClickListener {
    TextView tv_demo_number;
    Button btn_demo_increase;
    Button btn_demo_decrease;
    Button btn_demo_pause;

    private Handler handler = new Handler(Looper.myLooper()) {
        @Override
        public void handleMessage(Message msg) {
            //要得到当前显示的数值
            int number = Integer.parseInt(tv_demo_number.getText().toString());
            switch (msg.what) {
                case 1:
                    if (number == 20) {
                        //设置暂停不可操作
                        btn_demo_pause.setEnabled(false);
                        Toast.makeText(HandlerDemo.this, "已经达到最大值", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    number++;
                    tv_demo_number.setText(number + "");
                    //发送延迟消息
                    handler.sendEmptyMessageDelayed(1, 1000);
                    break;
                case 2:
                    if (number == 1) {
                        btn_demo_pause.setEnabled(false);
                        Toast.makeText(HandlerDemo.this, "已经达到最小值", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    number--;
                    tv_demo_number.setText(number + "");
                    handler.sendEmptyMessageDelayed(2, 1000);
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_handler_demo);
        init();
    }

    private void init() {
        tv_demo_number = findViewById(R.id.tv_demo_number);
        btn_demo_increase = findViewById(R.id.btn_demo_increase);
        btn_demo_decrease = findViewById(R.id.btn_demo_decrease);
        btn_demo_pause = findViewById(R.id.btn_demo_pause);
        btn_demo_increase.setOnClickListener(this);
        btn_demo_decrease.setOnClickListener(this);
        btn_demo_pause.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if (view == btn_demo_increase) {
            btn_demo_increase.setEnabled(false);
            btn_demo_decrease.setEnabled(true);
            btn_demo_pause.setEnabled(true);
            //停止减少 移除未处理的减少的消息
            handler.removeMessages(2);
            handler.sendEmptyMessage(1);
        } else if (view == btn_demo_decrease) {
            btn_demo_decrease.setEnabled(false);
            btn_demo_increase.setEnabled(true);
            btn_demo_pause.setEnabled(true);
            handler.removeMessages(1);
            handler.sendEmptyMessage(2);
        } else if (view == btn_demo_pause) {
            //移除未处理的减少或者增加的消息
            handler.removeMessages(1);
            handler.removeMessages(2);
            btn_demo_decrease.setEnabled(true);
            btn_demo_increase.setEnabled(true);
            btn_demo_pause.setEnabled(false);
        }
    }
}

效果图

Handler实现计数器

 

本文地址:https://blog.csdn.net/we1less/article/details/107898113